D. Loop runs too many times causing temp to become NULL
Solution
Step 1: Analyze loop iterations
Loop runs i=1 to i<=4 (4 times), moving temp 4 nodes ahead. For insertion at position 4, temp should stop at position 3.
Step 2: Consequence of loop error
Temp moves too far, possibly NULL, causing runtime error when accessing temp->next.
Final Answer:
Loop runs too many times causing temp to become NULL -> Option D
Quick Check:
Loop must run position-2 times, not position times [OK]
Hint: Loop till position-2 to avoid NULL pointer [OK]
Common Mistakes:
Using <= instead of < in loop condition
Not checking for NULL before accessing next
Incorrect loop range for insertion
5. Given a singly linked list with nodes: 1 -> 3 -> 5 -> 7 -> null, write the steps to insert a new node with value 4 at position 3. What will be the final list?
hard
A. 1 -> 4 -> 3 -> 5 -> 7 -> null
B. 4 -> 1 -> 3 -> 5 -> 7 -> null
C. 1 -> 3 -> 4 -> 5 -> 7 -> null
D. 1 -> 3 -> 5 -> 7 -> 4 -> null
Solution
Step 1: Traverse to node before position 3
Start at head (1), move to next node (3) which is position 2. Stop here to insert after this node.
Step 2: Insert new node with value 4
Create new node 4, set its next to current node's next (5), then update current node's next to new node.
Final Answer:
1 -> 3 -> 4 -> 5 -> 7 -> null -> Option C
Quick Check:
Insert at position 3 places new node between 3 and 5 [OK]
Hint: Stop at position-1 node, insert new node after it [OK]
Common Mistakes:
Inserting at wrong position
Not updating pointers correctly
Confusing position counting from 0 or 1
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.