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), 3) previous pointer (points to the previous node).
Click to reveal answer
beginner
How do you initialize an empty doubly linked list in TypeScript?
You create a class with a head and tail pointer set to null, meaning the list has no nodes yet.
Click to reveal answer
intermediate
Why do we need 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 to quickly add or remove nodes at both ends and traverse the list in both directions.
Click to reveal answer
beginner
Show the basic TypeScript class structure for a doubly linked list node.
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } defines both next and prev as Node or null, which is correct for doubly linked list nodes.
Step 2: Verify constructor initialization
class Node { data: number; next: Node | null; prev: Node | null; constructor(data: number) { this.data = data; this.next = null; this.prev = null; } } initializes next and prev to null, which is proper for a new node.
Hint: Look for both next and prev properties initialized to null [OK]
Common Mistakes:
Missing prev pointer in node class
Using wrong types for pointers
Initializing pointers with numbers instead of null
3. What will be the output of the following TypeScript code that creates a doubly linked list with nodes 10, 20, 30 and prints the list forward?
class Node {
data: number;
next: Node | null;
prev: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
head: Node | null = null;
append(data: number) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
newNode.prev = current;
}
printForward() {
let current = this.head;
let result = '';
while (current) {
result += current.data + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new DoublyLinkedList();
list.append(10);
list.append(20);
list.append(30);
list.printForward();
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. null -> 10 -> 20 -> 30
D. 10 -> 20 -> null
Solution
Step 1: Append nodes 10, 20, 30 in order
Nodes are added at the end, so list order is 10, then 20, then 30.
Step 2: Print list forward
printForward traverses from head to next nodes, printing data with arrows, ending with null.
Final Answer:
10 -> 20 -> 30 -> null -> Option A
Quick Check:
Appending adds to tail, printForward shows 10 to 30 [OK]
Hint: Appending adds nodes at tail; printForward shows head to tail [OK]
Common Mistakes:
Confusing print order as reversed
Missing last node in output
Printing null at start instead of end
4. Identify the error in the following TypeScript code that tries to append a node to a doubly linked list:
class Node {
data: number;
next: Node | null;
prev: Node | null;
constructor(data: number) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class DoublyLinkedList {
head: Node | null = null;
append(data: number) {
const newNode = new Node(data);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
// Missing line here
}
}
medium
A. The code misses setting newNode.prev to current before appending.
B. The code should set head to newNode even if head exists.
C. The constructor should initialize next and prev to undefined, not null.
D. The while loop should check current.prev instead of current.next.
Solution
Step 1: Check append logic for doubly linked list
When appending, newNode.prev must point to current (the last node) to maintain backward link.
Step 2: Identify missing pointer assignment
The code sets current.next = newNode but does not set newNode.prev = current, breaking backward link.
Final Answer:
The code misses setting newNode.prev to current before appending. -> Option A
Quick Check:
newNode.prev must link to previous node [OK]
Hint: Always set newNode.prev = last node when appending [OK]
Common Mistakes:
Forgetting to set newNode.prev
Changing head incorrectly
Checking wrong pointer in loop
5. You want to create a doubly linked list from an array of numbers [5, 15, 25] and then reverse the list by changing pointers (not data). Which sequence of pointer changes correctly reverses the list?
hard
A. For each node, set next to prev and leave prev unchanged; head remains same.
B. For each node, set next to null and prev to null; then append nodes in reverse order.
C. For each node, swap its next and prev pointers; then update head to last node.
D. For each node, swap data values instead of pointers; head stays unchanged.
Solution
Step 1: Understand reversing a doubly linked list by pointers
Reversing means for each node, next and prev pointers must be swapped to reverse direction.
Step 2: Update head pointer
After swapping pointers, head must point to the original tail (last node) to reflect new start.
Final Answer:
For each node, swap its next and prev pointers; then update head to last node. -> Option C
Quick Check:
Swap next and prev, then reset head [OK]
Hint: Swap next and prev pointers on all nodes, then reset head [OK]
Common Mistakes:
Only swapping data, not pointers
Not updating head after reversal
Setting pointers to null incorrectly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.