[双向链表]双向链表的Java实现
程序员文章站
2022-03-24 18:22:34
...
双向链表中,除了一个保存值的 val,还有一个额外引用字段保存前一个节点,一个额外引用字段保存后一个节点。
节点结构
/**
* 双向链表节点
*/
private class DoubleLinkedNode {
/**
* 节点 val
*/
private int val;
/**
* 前一个节点
*/
private DoubleLinkedNode prev;
/**
* 后一个节点
*/
private DoubleLinkedNode next;
/**
* 构造方法
* @param val val
*/
public DoubleLinkedNode(int val) {
this.val = val;
}
/**
* getVal 方法
* @return val
*/
public int getVal() {
return val;
}
}
大多数情况下,使用头节点来表示整个链表。
操作
访问
无法随机访问单链表的元素。如果要遍历第 i 个元素,必须逐个遍历。
找到需要访问的节点,返回 val
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if (index < 0 || index >= size) {
return -1;
}
DoubleLinkedNode temp = head.next;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp.getVal();
}
添加
头插法(逆序)
由于尾节点的存在,不需要判断 null
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void addAtHead(int val) {
DoubleLinkedNode pred = head;
DoubleLinkedNode next = pred.next;
DoubleLinkedNode curr = new DoubleLinkedNode(val);
curr.prev = pred;
curr.next = next;
pred.next = curr;
next.prev = curr;
size++;
}
尾插法(顺序)
由于需要保存插入位置的原始前后节点,所以要遍历到 next.next != null。
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
DoubleLinkedNode pred = head;
while (pred.next.next != null) {
pred = pred.next;
}
DoubleLinkedNode curr = new DoubleLinkedNode(val);
DoubleLinkedNode next = pred.next;
curr.prev = pred;
curr.next = next;
pred.next = curr;
next.prev = curr;
size++;
}
下标插入
遍历到插入节点的前一个节点,然后插入。
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if (index < 0 || index > size) {
return;
}
DoubleLinkedNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
DoubleLinkedNode curr = new DoubleLinkedNode(val);
DoubleLinkedNode next = pred.next;
curr.prev = pred;
curr.next = next;
pred.next = curr;
next.prev = curr;
size++;
}
删除
需要保存删除节点的原前后节点,故
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
DoubleLinkedNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
DoubleLinkedNode next = pred.next.next;
pred.next = next;
next.prev = pred;
size--;
}
设计双向链表
class MyLinkedList {
/**
* 链表大小
*/
private int size;
/**
* 头节点
*/
private DoubleLinkedNode head;
/**
* 尾节点
*/
private DoubleLinkedNode tail;
/** Initialize your data structure here. */
public MyLinkedList() {
head = new DoubleLinkedNode(0);
tail = new DoubleLinkedNode(0);
size = 0;
head.next = tail;
tail.prev = head;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if (index < 0 || index >= size) {
return -1;
}
DoubleLinkedNode temp = head.next;
for (int i = 0; i < index; i++) {
temp = temp.next;
}
return temp.getVal();
}
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
public void addAtHead(int val) {
DoubleLinkedNode pred = head;
DoubleLinkedNode next = pred.next;
DoubleLinkedNode curr = new DoubleLinkedNode(val);
curr.prev = pred;
curr.next = next;
pred.next = curr;
next.prev = curr;
size++;
}
/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
DoubleLinkedNode pred = head;
while (pred.next.next != null) {
pred = pred.next;
}
DoubleLinkedNode curr = new DoubleLinkedNode(val);
DoubleLinkedNode next = pred.next;
curr.prev = pred;
curr.next = next;
pred.next = curr;
next.prev = curr;
size++;
}
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if (index < 0 || index > size) {
return;
}
DoubleLinkedNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
DoubleLinkedNode curr = new DoubleLinkedNode(val);
DoubleLinkedNode next = pred.next;
curr.prev = pred;
curr.next = next;
pred.next = curr;
next.prev = curr;
size++;
}
/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index < 0 || index >= size) {
return;
}
DoubleLinkedNode pred = head;
for (int i = 0; i < index; i++) {
pred = pred.next;
}
DoubleLinkedNode next = pred.next.next;
pred.next = next;
next.prev = pred;
size--;
}
/**
* 双向链表节点
*/
private class DoubleLinkedNode {
/**
* 节点 val
*/
private int val;
/**
* 前一个节点
*/
private DoubleLinkedNode prev;
/**
* 后一个节点
*/
private DoubleLinkedNode next;
/**
* 构造方法
* @param val val
*/
public DoubleLinkedNode(int val) {
this.val = val;
}
/**
* getVal 方法
* @return val
*/
public int getVal() {
return val;
}
}
}
/**
* Your MyLinkedList object will be instantiated and called as such:
* MyLinkedList obj = new MyLinkedList();
* int param_1 = obj.get(index);
* obj.addAtHead(val);
* obj.addAtTail(val);
* obj.addAtIndex(index,val);
* obj.deleteAtIndex(index);
*/