Design Doubly Linked List
Let's briefly review the structure definition of a node in the doubly linked list.
Based on this definition, we are going to give you the solution step by step. The solution for the doubly linked list is similar to the one using singly linked list.
// Definition for doubly-linked list.
class DoublyListNode {
int val;
DoublyListNode next, prev;
DoublyListNode(int x) {val = x;}
}1. Initiate a new linked list: represent a linked list using the head node.
class MyLinkedList {
private DoublyListNode head;
/** Initialize your data structure here. */
public MyLinkedList() {
head = null;
}
}2. Traverse the linked list to get element by index.
/** 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;
}3. Add a new node.
Similar to the singly linked list, it takes O(N) time to get a node by index, where N is the length of the linked list. It is different from adding a new node after a given node.
5. Delete a node.
Similar to the add operation, it takes O(N) time to get the node by the index which is different from deleting a given node. However, it is different to the singly linked list. When we get the node we want to delete, we don't need to traverse to get its previous node but using the "prev" field instead.
Last updated
Was this helpful?