What is the main idea behind reversing a singly linked list iteratively?
We change the direction of the links between nodes one by one, using three pointers: previous, current, and next. This way, we reverse the list without creating new nodes.
Click to reveal answer
beginner
In the iterative reversal of a singly linked list, what role does the 'previous' pointer play?
The 'previous' pointer keeps track of the node that should come before the current node once the link is reversed. It starts as nil and moves forward as we reverse links.
Click to reveal answer
beginner
Why do we need a 'next' pointer during the iterative reversal of a singly linked list?
Because once we reverse the current node's link, we lose the original next node. The 'next' pointer saves the original next node before reversing the link, so we can continue traversing.
Click to reveal answer
beginner
What is the time complexity of reversing a singly linked list iteratively?
The time complexity is O(n), where n is the number of nodes, because we visit each node exactly once to reverse the links.
Click to reveal answer
beginner
What will be the new head of the list after reversing a singly linked list iteratively?
The new head will be the last node of the original list, which is tracked by the 'previous' pointer after the iteration finishes.
Click to reveal answer
Which pointer is used to keep track of the next node before reversing the link?
Anext
Bprevious
Ccurrent
Dhead
✗ Incorrect
The 'next' pointer stores the original next node before reversing the current node's link.
What is the initial value of the 'previous' pointer when starting the iterative reversal?
Ahead
Btail
Cnil (or null)
Dcurrent
✗ Incorrect
The 'previous' pointer starts as nil because there is no node before the head initially.
After reversing the list, which pointer points to the new head?
Aprevious
Bnext
Ccurrent
Dtail
✗ Incorrect
The 'previous' pointer ends up pointing to the new head after all links are reversed.
What is the time complexity of reversing a singly linked list iteratively?
AO(1)
BO(n)
CO(n^2)
DO(log n)
✗ Incorrect
We visit each node once, so the time complexity is O(n).
Which of these is NOT needed to reverse a singly linked list iteratively?
Aprevious pointer
Bcurrent pointer
Cnext pointer
Dextra array to store nodes
✗ Incorrect
No extra array is needed; reversal is done by changing links using pointers.
Explain step-by-step how to reverse a singly linked list using iteration.
Think about how pointers move and links change in each step.
You got /8 concepts.
Why is it important to save the next node before changing the current node's link during reversal?
Consider what happens if you reverse a link without saving the next node.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the iterative method to reverse a singly linked list?
easy
A. To sort the linked list in ascending order
B. To create a new linked list with reversed elements
C. To delete all nodes and recreate them in reverse order
D. To change the direction of the links between nodes one by one
Solution
Step 1: Understand the linked list structure
A singly linked list has nodes pointing to the next node only, so reversing means changing these pointers.
Step 2: Purpose of iterative reversal
The iterative method changes the next pointer of each node to point to the previous node, reversing the list in place.
Final Answer:
To change the direction of the links between nodes one by one -> Option D
Quick Check:
Reversing means changing links direction [OK]
Hint: Reversing means flipping links, not creating new nodes [OK]
Common Mistakes:
Thinking it creates a new list instead of reversing in place
Confusing reversal with sorting
Assuming nodes are deleted and recreated
2. Which of the following Go code snippets correctly initializes pointers for iterative reversal of a singly linked list?
easy
A. var prev = head; current := nil
B. var prev *Node = nil; current := head
C. prev := head; current := head.next
D. var prev *Node = head; current := head
Solution
Step 1: Initialization for reversal
We start with prev as nil because the new tail will point to nil, and current at head to traverse the list.
Step 2: Check each option
var prev *Node = nil; current := head correctly sets prev to nil and current to head. Others incorrectly assign prev or current.
Final Answer:
var prev *Node = nil; current := head -> Option B
Quick Check:
Prev starts nil, current at head [OK]
Hint: Prev starts nil, current starts at head [OK]
Common Mistakes:
Setting prev to head initially
Setting current to nil at start
Mixing pointer assignments
3. What will be the output linked list after running this Go code to reverse a singly linked list?
type Node struct {
Val int
Next *Node
}
func reverseList(head *Node) *Node {
var prev *Node = nil
current := head
for current != nil {
nextTemp := current.Next
current.Next = prev
prev = current
current = nextTemp
}
return prev
}
// Input list: 1 -> 2 -> 3 -> nil
// Call: reverseList(head)
medium
A. 3 -> 2 -> 1 -> nil
B. 1 -> 2 -> 3 -> nil
C. nil -> 3 -> 2 -> 1
D. 3 -> 1 -> 2 -> nil
Solution
Step 1: Trace the reversal loop
Initially prev=nil, current=1. Each iteration reverses current.Next to prev and moves pointers forward.
Step 2: Final reversed list
After loop, prev points to node with Val=3, linked as 3->2->1->nil.
Final Answer:
3 -> 2 -> 1 -> nil -> Option A
Quick Check:
Reversed list order is 3,2,1 [OK]
Hint: Follow pointer changes step-by-step [OK]
Common Mistakes:
Confusing original and reversed order
Misunderstanding pointer updates
Assuming list remains unchanged
4. Identify the error in this Go code snippet for reversing a singly linked list iteratively:
func reverseList(head *Node) *Node {
var prev *Node = head
current := head
for current != nil {
nextTemp := current.Next
current.Next = prev
prev = current
current = nextTemp
}
return prev
}
medium
A. prev should be initialized to nil, not head
B. current should be initialized to nil
C. nextTemp should be initialized outside the loop
D. The function should return current, not prev
Solution
Step 1: Check initialization of prev
Prev must start as nil because the new tail's next is nil. Setting prev=head causes a cycle.
Step 2: Effect of wrong prev initialization
Setting prev=head makes current.Next point to itself on first iteration, causing infinite loop or wrong reversal.
Final Answer:
prev should be initialized to nil, not head -> Option A
Quick Check:
Prev starts nil to avoid cycles [OK]
Hint: Prev must start nil to break old links [OK]
Common Mistakes:
Initializing prev to head causing cycles
Returning current instead of prev
Misplacing nextTemp initialization
5. Given a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> nil, write the final linked list after applying iterative reversal twice in a row.