Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Insert at Beginning Head Insert in DSA Typescript - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Beginning Head Insert
Create new node
↓
New node.next = head
↓
head = new node
↓
Done
Insert a new node by creating it, pointing it to the current head, then updating head to this new node.
Execution Sample
DSA Typescript
class Node {
  data: number;
  next: Node | null;
  constructor(data: number) {
    this.data = data;
    this.next = null;
  }
}

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

// Example usage:
let head: Node | null = null;
head = insertAtBeginning(head, 10);
head = insertAtBeginning(head, 20);
This code inserts nodes at the beginning of a singly linked list, updating the head each time.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Insert 10 at beginningNone → 1 nodenewNode.next = null (head was null), head = newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:10│ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → newNode
2Insert 20 at beginning1 node → 2 nodesnewNode.next = old head (node 10), head = newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:20│ │ data:10│ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → newNode
3Insert 30 at beginning2 nodes → 3 nodesnewNode.next = old head (node 20), head = newNodeā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā” │ data:30│ │ data:20│ │ data:10│ │ next:──┼──→ │ next:──┼──→ │ next:āˆ… │ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜ head → newNode
4End of insertions3 nodesNo pointer changesFinal list: 30 → 20 → 10 → null
šŸ’” No more insertions; head points to newest node at start.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
headnullNode(10)Node(20)Node(30)Node(30)
newNodeundefinedNode(10)Node(20)Node(30)Node(30)
newNode.nextundefinednullNode(10)Node(20)Node(20)
Key Moments - 3 Insights
Why does newNode.next point to the old head instead of null after the first insertion?
Because after the first insertion, head points to the node with data 10. When inserting 20, newNode.next is set to the current head (node 10), linking the new node before it. See execution_table step 2.
Why do we update head to newNode after setting newNode.next?
We update head to newNode so the list starts at the new node. If we didn't update head, the list would still start at the old node, losing the new node at the beginning. See execution_table pointer changes.
What happens if the list is empty (head is null) when inserting?
If head is null, newNode.next is set to null, making the new node the only node. Head then points to this new node. See execution_table step 1.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does head point to after step 2?
ANode with data 10
BNode with data 20
CNode with data 30
Dnull
šŸ’” Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 2.
At which step does the list first have more than one node?
AStep 1
BStep 3
CStep 2
DStep 4
šŸ’” Hint
Look at the 'Nodes in List' column in the execution_table.
If we insert a new node with data 40 after step 3, what will head point to?
ANode with data 40
BNode with data 10
CNode with data 30
DNode with data 20
šŸ’” Hint
Recall that insert at beginning updates head to the new node.
Concept Snapshot
Insert at Beginning (Head Insert):
- Create new node with data
- Set newNode.next = current head
- Update head = newNode
- New node becomes list start
- Runs in O(1) time
- List grows at front
Full Transcript
Inserting at the beginning of a singly linked list means creating a new node, pointing it to the current head, and then updating head to this new node. This operation adds the new node at the start of the list. If the list is empty, the new node points to null and becomes the head. Each insertion updates the head pointer to the new node, so the list order is newest node first. This is efficient because it does not require traversal. The visual steps show how pointers change and how the list grows from empty to multiple nodes.

Practice

(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Adds a new node at the start of the list
B. Adds a new node at the end of the list
C. Deletes the first node of the list
D. Deletes the last node of the list

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means adding a new node before the current first node.
  2. Step 2: Effect on the list

    The new node becomes the head, pushing the old head to second position.
  3. Final Answer:

    Adds a new node at the start of the list -> Option A
  4. Quick Check:

    Insert at beginning = Add new head [OK]
Hint: Insert at beginning means new head node [OK]
Common Mistakes:
  • Confusing insert at beginning with insert at end
  • Thinking it deletes nodes
  • Assuming it changes the tail node
2. Which of the following is the correct TypeScript syntax to insert a new node at the beginning of a singly linked list?
easy
A. newNode = head; head.next = newNode;
B. head = newNode.next; newNode = head;
C. head.next = newNode; newNode = head;
D. newNode.next = head; head = newNode;

Solution

  1. Step 1: Link new node to current head

    Set newNode.next to point to current head node.
  2. Step 2: Update head to new node

    Assign head to newNode so it becomes the first node.
  3. Final Answer:

    newNode.next = head; head = newNode; -> Option D
  4. Quick Check:

    Link new node to head, then update head [OK]
Hint: Set newNode.next to head, then head = newNode [OK]
Common Mistakes:
  • Assigning head before linking newNode.next
  • Swapping assignments order
  • Using head.next instead of newNode.next
3. What will be the linked list after executing the following TypeScript code?
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

let head: Node | null = new Node(10);
let newNode = new Node(5);
newNode.next = head;
head = newNode;
medium
A. 10 -> null
B. 10 -> 5 -> null
C. 5 -> 10 -> null
D. 5 -> null

Solution

  1. Step 1: Initial list setup

    head points to node with value 10, next is null.
  2. Step 2: Insert newNode at beginning

    newNode (5) points to head (10), then head updated to newNode.
  3. Final Answer:

    5 -> 10 -> null -> Option C
  4. Quick Check:

    New node 5 becomes head, points to 10 [OK]
Hint: New node points to old head, then becomes new head [OK]
Common Mistakes:
  • Confusing order of nodes after insertion
  • Forgetting to update head
  • Assuming newNode.next is null
4. Identify the error in this TypeScript code that tries to insert a node at the beginning of a linked list:
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

let head: Node | null = null;
let newNode = new Node(7);
head.next = newNode;
head = newNode;
medium
A. head is null, so head.next causes an error
B. newNode is not linked to head
C. head should be assigned before newNode.next
D. Missing constructor call for newNode

Solution

  1. Step 1: Check initial head value

    head is null, so accessing head.next is invalid and causes runtime error.
  2. Step 2: Correct insertion approach

    Assign newNode.next = head (which is null), then head = newNode.
  3. Final Answer:

    head is null, so head.next causes an error -> Option A
  4. Quick Check:

    Cannot access property on null head [OK]
Hint: Check if head is null before accessing head.next [OK]
Common Mistakes:
  • Trying to access next on null head
  • Not linking newNode.next to head
  • Assigning head after using head.next
5. Given a singly linked list with nodes 20 -> 30 -> 40 -> null, what will be the list after inserting 10 at the beginning using head insert method?
hard
A. 20 -> 30 -> 40 -> 10 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 30 -> 40 -> 10 -> 20 -> null
D. 10 -> 30 -> 40 -> 20 -> null

Solution

  1. Step 1: Understand initial list

    List is 20 -> 30 -> 40 -> null, head points to 20.
  2. Step 2: Insert 10 at beginning

    Create new node 10, set newNode.next = head (20), then head = newNode.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> null -> Option B
  4. Quick Check:

    New node 10 becomes head, old list follows [OK]
Hint: New node points to old head, becomes new head [OK]
Common Mistakes:
  • Appending new node at end instead of beginning
  • Mixing node order after insertion
  • Not updating head 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