Challenge - 5 Problems
Master of Iterative Linked List Reversal
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of reversing a 3-node linked list
What is the printed state of the linked list after reversing it iteratively?
DSA Go
package main import "fmt" type Node struct { value 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 } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.value) head = head.next } fmt.Println("null") } func main() { head := &Node{value: 1} head.next = &Node{value: 2} head.next.next = &Node{value: 3} reversed := reverseList(head) printList(reversed) }
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in the iterative reversal.
✗ Incorrect
The iterative reversal changes the next pointers of each node to point to the previous node, reversing the list order from 1->2->3 to 3->2->1.
❓ Predict Output
intermediateOutput when reversing an empty linked list
What is the printed state of the linked list after reversing an empty list iteratively?
DSA Go
package main import "fmt" type Node struct { value 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 } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.value) head = head.next } fmt.Println("null") } func main() { var head *Node = nil reversed := reverseList(head) printList(reversed) }
Attempts:
2 left
💡 Hint
Consider what happens when the input list is nil.
✗ Incorrect
Reversing an empty list returns nil, so printing it results in just 'null'.
🔧 Debug
advancedIdentify the bug in this iterative reversal code
What error will this code cause when reversing a linked list iteratively?
DSA Go
func reverseList(head *Node) *Node {
var prev *Node = nil
current := head
for current.next != nil {
nextTemp := current.next
current.next = prev
prev = current
current = nextTemp
}
return prev
}Attempts:
2 left
💡 Hint
Check the loop condition and what happens when current is the last node.
✗ Incorrect
The loop condition 'current.next != nil' skips processing the last node, so when current becomes nil, accessing current.next causes a nil pointer dereference.
❓ Predict Output
advancedOutput after reversing a single-node linked list
What is the printed state of the linked list after reversing a single-node list iteratively?
DSA Go
package main import "fmt" type Node struct { value 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 } func printList(head *Node) { for head != nil { fmt.Printf("%d -> ", head.value) head = head.next } fmt.Println("null") } func main() { head := &Node{value: 42} reversed := reverseList(head) printList(reversed) }
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
✗ Incorrect
A single node reversed still points to nil, so the list remains '42 -> null'.
🧠 Conceptual
expertWhy does iterative reversal use three pointers?
In iterative reversal of a singly linked list, why do we need to keep track of three pointers: prev, current, and nextTemp?
Attempts:
2 left
💡 Hint
Think about what happens if you change a link without saving the next node.
✗ Incorrect
We use prev to point to the reversed part, current to the node being processed, and nextTemp to save the next node before changing links. This prevents losing access to the rest of the list.
