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 steps are needed to insert a new node at the beginning of a doubly linked list?
1. Create a new node with the given data. 2. Set the new node's next pointer to the current head. 3. Set the new node's previous pointer to null. 4. If the list is not empty, set the current head's previous pointer to the new node. 5. Update the head to be the new node.
Click to reveal answer
beginner
Why do we need to update the previous pointer of the old head when inserting at the beginning?
Because the new node becomes the first node, the old head now has a node before it. So, its previous pointer must point to the new node to keep the list connected both ways.
Click to reveal answer
beginner
What happens if the doubly linked list is empty when inserting at the beginning?
The new node becomes the only node in the list. Its next and previous pointers are both null, and the head points to this new node.
Click to reveal answer
intermediate
Show the TypeScript code snippet to insert a node at the beginning of a doubly linked list.
3. Given the following TypeScript code for a doubly linked list insertion at beginning, what will be the printed list after insertion?
class Node {
data: number;
next: Node | null = null;
prev: Node | null = null;
constructor(data: number) { this.data = data; }
}
let head: Node | null = new Node(10);
head.next = new Node(20);
head.next.prev = head;
const newNode = new Node(5);
newNode.next = head;
head.prev = newNode;
head = newNode;
// Print list
let temp = head;
const result: number[] = [];
while(temp) {
result.push(temp.data);
temp = temp.next;
}
console.log(result);
medium
A. [5, 10, 20]
B. [10, 20, 5]
C. [20, 10, 5]
D. [5, 20, 10]
Solution
Step 1: Initial list before insertion
List is 10 <-> 20, head points to 10.
Step 2: Insert newNode with data 5 at beginning
newNode.next = head (10), head.prev = newNode, head = newNode (5).
Step 3: Traverse list from new head
Traverse from 5: 5 -> 10 -> 20.
Final Answer:
[5, 10, 20] -> Option A
Quick Check:
New head 5 followed by 10 and 20 [OK]
Hint: New node becomes head, list order starts with new node [OK]
Common Mistakes:
Not updating head to newNode
Incorrectly linking prev pointers
Printing list from old head
4. Identify the error in the following TypeScript code that attempts to insert a node at the beginning of a doubly linked list:
const newNode = new Node(15);
newNode.prev = null;
newNode.next = head;
head.prev = newNode;
head = newNode;
medium
A. newNode.prev should point to head
B. Missing check if head is null before accessing head.prev
C. head should be assigned before setting newNode.next
D. newNode.next should be null
Solution
Step 1: Check if head is null before accessing head.prev
If the list is empty (head is null), accessing head.prev causes an error.
Step 2: Add condition to update head.prev only if head exists
Use if(head) head.prev = newNode; to avoid runtime error.
Final Answer:
Missing check if head is null before accessing head.prev -> Option B
Quick Check:
Always check head before head.prev [OK]
Hint: Check if head exists before accessing its properties [OK]
Common Mistakes:
Ignoring null head and causing runtime error
Swapping next and prev pointers
Assigning head too early
5. You have a doubly linked list with nodes containing values [30, 40, 50]. You want to insert a new node with value 20 at the beginning. After insertion, which of the following correctly represents the list and the pointers of the new head node?
hard
A. List: 20 <-> 30 <-> 40 <-> 50; new head prev points to 30, next = null
B. List: 30 <-> 40 <-> 50 <-> 20; new head prev = 50, next = null
C. List: 20 <-> 30 <-> 40 <-> 50; new head prev = null, next points to 30
D. List: 50 <-> 40 <-> 30 <-> 20; new head prev = null, next points to 40
Solution
Step 1: Insert new node with value 20 before current head 30
New node becomes head, its prev is null, next points to old head (30).
Step 2: Update old head's prev to new node
Old head (30) prev pointer updated to new node (20).
Step 3: Confirm list order and pointers
List order is 20 <-> 30 <-> 40 <-> 50 with new head prev=null, next=30.
Final Answer:
List: 20 <-> 30 <-> 40 <-> 50; new head prev = null, next points to 30 -> Option C
Quick Check:
New head prev=null, next=old head [OK]
Hint: New head prev always null, next points to old head [OK]
Common Mistakes:
Setting new head prev to old tail
Reversing list order accidentally
Not updating old head prev pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.