Design Doubly Linked List
// Definition for doubly-linked list.
class DoublyListNode {
int val;
DoublyListNode next, prev;
DoublyListNode(int x) {val = x;}
}class MyLinkedList {
private DoublyListNode head;
/** Initialize your data structure here. */
public MyLinkedList() {
head = null;
}
}/** Helper function to return the index-th node in the linked list. */
private DoublyListNode getNode(int index) {
DoublyListNode cur = head;
for (int i = 0; i < index && cur != null; ++i) {
cur = cur.next;
}
return cur;
}
/** Helper function to return the last node in the linked list. */
private DoublyListNode getTail() {
DoublyListNode cur = head;
while (cur != null && cur.next != null) {
cur = cur.next;
}
return cur;
}
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
DoublyListNode cur = getNode(index);
return cur == null ? -1 : cur.val;
}Last updated