A doubly linked list is a chain of nodes where each node has three parts: data, a pointer to the next node, and a pointer to the previous node. This allows moving forward and backward through the list.
Click to reveal answer
beginner
What are the main parts of a node in a doubly linked list?
Each node has: 1) data (the value stored), 2) next pointer (points to the next node), and 3) previous pointer (points to the previous node).
Click to reveal answer
beginner
How do you initialize an empty doubly linked list in JavaScript?
You create a class for the list with a head and tail pointer set to null, meaning the list has no nodes yet.
Click to reveal answer
intermediate
Why do we keep both head and tail pointers in a doubly linked list?
Head points to the first node and tail points to the last node. This helps quickly add or remove nodes at both ends without traversing the whole list.
Click to reveal answer
beginner
Show the basic JavaScript code to create a node for a doubly linked list.
A. The constructor is missing initialization of head and tail.
B. The newNode should not have prev or next pointers.
C. The line 'this.tail.prev = newNode;' should be 'newNode.prev = this.tail;'.
D. The append method should not update the tail pointer.
Solution
Step 1: Analyze pointer updates in append method
When adding newNode, the previous tail's next should point to newNode, and newNode's prev should point to old tail.
Step 2: Identify incorrect pointer assignment
The code incorrectly sets 'this.tail.prev = newNode;' which wrongly changes the old tail's prev pointer instead of newNode's prev pointer.
Final Answer:
The line 'this.tail.prev = newNode;' should be 'newNode.prev = this.tail;'. -> Option C
Quick Check:
Correct prev pointer must link newNode to old tail [OK]
Hint: Prev pointer goes from new node back to old tail [OK]
Common Mistakes:
Swapping prev and next pointers
Not updating newNode.prev correctly
Forgetting to update tail pointer
5. You want to create a doubly linked list from an array of values [10, 20, 30] and then traverse it backward from tail to head. Which code snippet correctly initializes the list and prints the values backward?
hard
A. const list = new DoublyLinkedList();
for (const val of [10, 20, 30]) list.append(val);
let current = list.tail;
while (current) { console.log(current.data); current = current.prev; }
B. const list = new DoublyLinkedList();
for (let i = 2; i >= 0; i--) list.append([10, 20, 30][i]);
let current = list.head;
while (current) { console.log(current.data); current = current.next; }
C. const list = new DoublyLinkedList();
for (const val of [10, 20, 30]) list.append(val);
let current = list.head;
while (current) { console.log(current.data); current = current.prev; }
D. const list = new DoublyLinkedList();
for (const val of [10, 20, 30]) list.append(val);
let current = list.tail;
while (current) { console.log(current.data); current = current.next; }
Solution
Step 1: Append values in order to the list
Using a for-of loop appends 10, then 20, then 30, building the list head to tail.
Step 2: Traverse backward from tail using prev pointer
Starting at tail (30), moving prev prints 30, 20, 10 in reverse order.
Final Answer:
const list = new DoublyLinkedList();
for (const val of [10, 20, 30]) list.append(val);
let current = list.tail;
while (current) { console.log(current.data); current = current.prev; } -> Option A
Quick Check:
Backward traversal uses tail and prev pointer [OK]
Hint: Traverse backward from tail using prev pointer [OK]
Common Mistakes:
Traversing backward from head
Using next pointer to go backward
Appending values in reverse order but traversing forward
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.