Bird
Raised Fist0
DSA Javaprogramming~10 mins

Insert at Beginning Head Insert in DSA Java - Execution Trace

Choose your learning style10 modes available
Concept Flow - Insert at Beginning Head Insert
Create new node
↓
Set new_node.next = head
↓
Update head = new_node
↓
Done
Insert a new node by creating it, linking it to the current head, then updating head to this new node.
Execution Sample
DSA Java
Node newNode = new Node(5);
newNode.next = head;
head = newNode;
Insert a new node with value 5 at the beginning of the linked list.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create new node with data=5nullnewNode → Node(5, next=null)null (empty list)
2Set newNode.next = head (null)nullnewNode.next = nullnewNode(5) → ∅
3Update head = newNodeNode(5)head → newNodehead → Node(5) → ∅
4Insert another node with data=10Node(5)newNode → Node(10, next=null)head → Node(5) → ∅
5Set newNode.next = head (Node(5))Node(5)newNode.next = headnewNode(10) → Node(5) → ∅
6Update head = newNodeNode(10) → Node(5)head → newNodehead → Node(10) → Node(5) → ∅
7TerminationNode(10) → Node(5)No more operationsFinal list: head → 10 → 5 → ∅
💡 Insertion complete, head points to the newest node at beginning.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
headnullnullnullNode(5)Node(5)Node(5)Node(10)Node(10)
newNodenullNode(5)Node(5)Node(5)Node(10)Node(10)Node(10)Node(10)
newNode.nextnullnullnullnullnullNode(5)Node(5)Node(5)
Key Moments - 3 Insights
Why do we set newNode.next = head before updating head?
Because we want the new node to point to the current first node before head changes. See step 2 and 3 in execution_table where newNode.next links to old head, then head updates.
What happens if the list is empty before insertion?
Head is null initially (step 1). Setting newNode.next = head means newNode.next = null, so the new node becomes the only node (step 3).
Why does head point to newNode after insertion?
Because we want the new node to be the first node in the list. Step 3 shows head updated to newNode, making it the new start.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does head point to?
ANode with data 5
Bnull
CNode with data 10
DNode with data 0
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 3.
At which step does the list first contain two nodes?
AStep 3
BStep 6
CStep 5
DStep 4
💡 Hint
Look at 'Nodes in List' and 'Visual State' columns to see when two nodes appear.
If we skip setting newNode.next = head, what happens to the list?
AHead points to old node
BList remains unchanged
CNew node points to null, losing old list
DNew node points to itself
💡 Hint
Refer to step 2 where newNode.next is set to head to keep the list linked.
Concept Snapshot
Insert at Beginning (Head Insert):
- Create new node
- Set newNode.next = head
- Update head = newNode
- New node becomes first in list
- Works even if list is empty
Full Transcript
To insert a node at the beginning of a linked list, first create the new node. Then link this new node's next pointer to the current head of the list. Finally, update the head pointer to this new node. This makes the new node the first node in the list. If the list was empty, head was null, so newNode.next becomes null and the new node is the only node. This process is shown step-by-step in the execution table and variable tracker.

Practice

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

Solution

  1. Step 1: Understand the operation

    Inserting at the beginning means placing 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 C
  4. Quick Check:

    Insert at beginning = Add new head [OK]
Hint: New node becomes head, old head moves next [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 Java syntax to insert a new node at the beginning of a singly linked list named list with value 5?
easy
A. list.insertEnd(5);
B. list.add(5);
C. list.head.next = new Node(5);
D. list.head = new Node(5, list.head);

Solution

  1. Step 1: Understand Node constructor

    Assuming Node constructor takes value and next node, new Node(5, list.head) creates a new node pointing to current head.
  2. Step 2: Assign new node as head

    Setting list.head = new Node(5, list.head); inserts at beginning correctly.
  3. Final Answer:

    list.head = new Node(5, list.head); -> Option D
  4. Quick Check:

    New node points to old head, becomes new head [OK]
Hint: New node points to old head, assign new node as head [OK]
Common Mistakes:
  • Using add() which may add at end
  • Changing next of head instead of head itself
  • Using insertEnd() which adds at tail
3. What will be the output linked list after executing the following Java code?
class Node {
  int data;
  Node next;
  Node(int d, Node n) { data = d; next = n; }
}

Node head = new Node(3, null);
head = new Node(2, head);
head = new Node(1, head);

// Print list
Node temp = head;
while(temp != null) {
  System.out.print(temp.data + " -> ");
  temp = temp.next;
}
System.out.print("null");
medium
A. 3 -> 2 -> 1 -> null
B. 1 -> 2 -> 3 -> null
C. null -> 1 -> 2 -> 3
D. 2 -> 1 -> 3 -> null

Solution

  1. Step 1: Trace insertions

    First node: 3 -> null
    Insert 2 at beginning: 2 -> 3 -> null
    Insert 1 at beginning: 1 -> 2 -> 3 -> null
  2. Step 2: Print list

    Loop prints nodes in order: 1, 2, 3, then null.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option B
  4. Quick Check:

    Head insertions reverse order: 1,2,3 [OK]
Hint: Head insert reverses order of inserted nodes [OK]
Common Mistakes:
  • Assuming insertion adds at end
  • Printing nodes in wrong order
  • Confusing next pointers
4. Identify the error in the following code snippet that tries to insert a new node with value 10 at the beginning of a singly linked list.
class Node {
  int data;
  Node next;
  Node(int d) { data = d; next = null; }
}

Node head = null;
Node newNode = new Node(10);
newNode.next = head.next;
head = newNode;
medium
A. Accessing head.next when head is null causes NullPointerException
B. New node is not assigned to head
C. Node constructor is missing next parameter
D. No error, code works fine

Solution

  1. Step 1: Analyze head initialization

    Head is null initially, so head.next access is invalid and causes error.
  2. Step 2: Identify cause of error

    Trying to assign newNode.next = head.next; throws NullPointerException because head is null.
  3. Final Answer:

    Accessing head.next when head is null causes NullPointerException -> Option A
  4. Quick Check:

    Null head means head.next invalid [OK]
Hint: Check if head is null before accessing head.next [OK]
Common Mistakes:
  • Ignoring null head before accessing next
  • Assuming constructor error
  • Thinking newNode not assigned to head
5. You have a singly linked list with nodes containing values [4, 5, 6] in that order. You want to insert a new node with value 3 at the beginning. After insertion, you want to print the list in the format shown below.

Which code snippet correctly performs the insertion and prints the list?
class Node {
  int data;
  Node next;
  Node(int d) { data = d; next = null; }
}

Node head = new Node(4);
head.next = new Node(5);
head.next.next = new Node(6);

// Insert new node with value 3 at beginning and print list
// Fill in the blanks below

// Insert code here

// Print code here
hard
A. Node newNode = new Node(3); newNode.next = head; head = newNode; Node temp = head; while(temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null");
B. Node newNode = new Node(3); head.next = newNode; newNode.next = null; Node temp = head; while(temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null");
C. Node newNode = new Node(3); head = newNode; newNode.next = head.next; Node temp = head; while(temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null");
D. Node newNode = new Node(3); newNode.next = null; head.next = newNode; Node temp = head; while(temp != null) { System.out.print(temp.data + " -> "); temp = temp.next; } System.out.print("null");

Solution

  1. Step 1: Insert new node at beginning

    Create new node with value 3, set its next to current head, then update head to new node.
  2. Step 2: Print the list

    Traverse from head, print each node's data followed by ' -> ', end with 'null'.
  3. Final Answer:

    Code snippet in Option A correctly inserts and prints the list -> Option A
  4. Quick Check:

    New node points to old head, head updated [OK]
Hint: New node next = old head; head = new node; print from head [OK]
Common Mistakes:
  • Linking new node incorrectly
  • Not updating head to new node
  • Appending new node at end instead of beginning
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