Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Insert at Beginning of Doubly Linked List in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Beginning of Doubly Linked List
Create new node
↓
Set new_node.next = head
↓
If head != null
Yes↓
Set head.prev = new_node
↓
Set head = new_node
↓
Done
The new node is created and linked before the current head. If the list is not empty, the old head's prev pointer is updated. Finally, head points to the new node.
Execution Sample
DSA Typescript
class Node {
  data: number;
  prev: Node | null;
  next: Node | null;
  constructor(data: number) {
    this.data = data; this.prev = null; this.next = null;
  }
}

function insertAtBeginning(head: Node | null, data: number): Node {
  const newNode = new Node(data);
  newNode.next = head;
  if (head !== null) head.prev = newNode;
  return newNode;
}

// Example usage:
let head = null;
head = insertAtBeginning(head, 10);
head = insertAtBeginning(head, 20);
This code inserts nodes at the beginning of a doubly linked list, updating pointers accordingly.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=10EmptynewNode created (data=10)āˆ… (empty list)
2Set newNode.next = head (null)EmptynewNode.next = nullnewNode(data=10) → āˆ…
3head is null, skip head.prev updateEmptyNo changenewNode(data=10) → āˆ…
4Set head = newNode1 nodehead → newNode(data=10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ prev:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
5Create new node with data=201 nodenewNode created (data=20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ data:20│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:āˆ… │ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
6Set newNode.next = head (node 10)1 nodenewNode.next → node(10)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│──→ │ data:10│ │ prev:āˆ… │ │ prev:āˆ… │ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ newNode old head
7head != null, set head.prev = newNode1 nodenode(10).prev → newNode(20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│──→ │ data:10│ │ prev:āˆ… │ │ prev:──┼ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ newNode old head
8Set head = newNode2 nodeshead → newNode(20)ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│──→ │ data:10│ │ prev:āˆ… │ │ prev:──┼ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
9End of insertion2 nodesPointers stableā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│──→ │ data:10│ │ prev:āˆ… │ │ prev:──┼ │ next:──┼ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head
šŸ’” Insertion complete; head points to new node with data=20, list has two nodes.
Variable Tracker
VariableStartAfter Step 1After Step 5After Step 8Final
headnullnullNode(10)Node(20)Node(20)
newNodeundefinedNode(10)Node(20)Node(20)Node(20)
newNode.nextundefinednullnullNode(10)Node(10)
head.prevundefinedundefinednullNode(20)Node(20)
Key Moments - 3 Insights
Why do we set newNode.next = head before updating head?
Because newNode must point to the current first node before head changes. See execution_table step 6 where newNode.next points to old head.
Why do we check if head is not null before setting head.prev?
If head is null (empty list), there is no node to update prev pointer. See step 3 where this check skips updating head.prev.
How does the prev pointer of the old head get updated?
In step 7, after newNode.next points to old head, we set old head's prev to newNode, linking backward.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what does newNode.next point to?
Aold head node with data 10
BnewNode itself
Cnull
Dhead.prev
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 6.
At which step does the old head's prev pointer get updated to the new node?
AStep 4
BStep 7
CStep 6
DStep 8
šŸ’” Hint
Look for the step mentioning 'head.prev = newNode' in the 'Operation' column.
If the list was initially empty, which step is skipped?
AStep 7
BStep 2
CStep 3
DStep 8
šŸ’” Hint
See the condition check for head != null before updating head.prev.
Concept Snapshot
Insert at Beginning of Doubly Linked List:
- Create new node with data
- Set newNode.next = current head
- If head exists, set head.prev = newNode
- Update head to newNode
- List grows at front, pointers updated both ways
Full Transcript
To insert a node at the beginning of a doubly linked list, first create a new node. Then set its next pointer to the current head. If the list is not empty, update the old head's prev pointer to the new node. Finally, update the head pointer to the new node. This ensures the new node is the first element, and both forward and backward links are correct.

Practice

(1/5)
1. What is the main purpose of inserting a node at the beginning of a doubly linked list?
easy
A. To reverse the entire list
B. To add a new node after the tail node
C. To delete the first node in the list
D. To add a new node before the current head node

Solution

  1. Step 1: Understand the head node position

    The head node is the first node in the doubly linked list.
  2. Step 2: Insert new node before head

    Inserting at the beginning means placing the new node before the current head, making it the new head.
  3. Final Answer:

    To add a new node before the current head node -> Option D
  4. Quick Check:

    Insert at beginning = add before head [OK]
Hint: Beginning means before the head node [OK]
Common Mistakes:
  • Confusing insertion at beginning with insertion at end
  • Thinking insertion deletes nodes
  • Mixing up head and tail nodes
2. Which of the following TypeScript code snippets correctly creates a new node and inserts it at the beginning of a doubly linked list?
easy
A. newNode.next = head; if(head) head.prev = newNode; head = newNode;
B. newNode.prev = head; if(head) head.next = newNode; head = newNode;
C. newNode.next = null; head = newNode; newNode.prev = null;
D. head = newNode; newNode.next = head; newNode.prev = head;

Solution

  1. Step 1: Set newNode.next to current head

    The new node's next pointer should point to the current head node.
  2. Step 2: Update current head's prev to newNode if head exists

    If the list is not empty, the current head's prev pointer must point back to the new node.
  3. Step 3: Update head to newNode

    Finally, the head pointer is updated to the new node.
  4. Final Answer:

    newNode.next = head; if(head) head.prev = newNode; head = newNode; -> Option A
  5. Quick Check:

    Set newNode.next=head, head.prev=newNode, head=newNode [OK]
Hint: newNode.next = head; head.prev = newNode; head = newNode [OK]
Common Mistakes:
  • Swapping next and prev pointers
  • Not updating head pointer
  • Setting newNode.prev incorrectly
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

  1. Step 1: Initial list before insertion

    List is 10 <-> 20, head points to 10.
  2. Step 2: Insert newNode with data 5 at beginning

    newNode.next = head (10), head.prev = newNode, head = newNode (5).
  3. Step 3: Traverse list from new head

    Traverse from 5: 5 -> 10 -> 20.
  4. Final Answer:

    [5, 10, 20] -> Option A
  5. 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

  1. 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.
  2. Step 2: Add condition to update head.prev only if head exists

    Use if(head) head.prev = newNode; to avoid runtime error.
  3. Final Answer:

    Missing check if head is null before accessing head.prev -> Option B
  4. 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

  1. 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).
  2. Step 2: Update old head's prev to new node

    Old head (30) prev pointer updated to new node (20).
  3. Step 3: Confirm list order and pointers

    List order is 20 <-> 30 <-> 40 <-> 50 with new head prev=null, next=30.
  4. Final Answer:

    List: 20 <-> 30 <-> 40 <-> 50; new head prev = null, next points to 30 -> Option C
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard