Bird
Raised Fist0
DSA Goprogramming~20 mins

Reverse a Singly Linked List Iterative in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
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
intermediate
2:00remaining
Output 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)
}
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
Cnull
D1 -> null
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in the iterative reversal.
❓ Predict Output
intermediate
2:00remaining
Output 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)
}
Aempty list error
Bnull
C0 -> null
Dnil pointer exception
Attempts:
2 left
💡 Hint
Consider what happens when the input list is nil.
🔧 Debug
advanced
2:00remaining
Identify 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
}
ARuntime error: nil pointer dereference
BCompilation error
CCorrect reversal output
DInfinite loop
Attempts:
2 left
💡 Hint
Check the loop condition and what happens when current is the last node.
❓ Predict Output
advanced
2:00remaining
Output 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)
}
ARuntime error
Bnull
C42 -> 42 -> null
D42 -> null
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
🧠 Conceptual
expert
2:00remaining
Why 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?
ATo create a new list instead of reversing in place
BTo sort the list while reversing
CTo reverse the links safely without losing access to the rest of the list
DTo count the number of nodes during reversal
Attempts:
2 left
💡 Hint
Think about what happens if you change a link without saving the next node.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To change the direction of the links between nodes one by one -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    var prev *Node = nil; current := head -> Option B
  4. 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

  1. Step 1: Trace the reversal loop

    Initially prev=nil, current=1. Each iteration reverses current.Next to prev and moves pointers forward.
  2. Step 2: Final reversed list

    After loop, prev points to node with Val=3, linked as 3->2->1->nil.
  3. Final Answer:

    3 -> 2 -> 1 -> nil -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    prev should be initialized to nil, not head -> Option A
  4. 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.
hard
A. 40 -> 30 -> 20 -> 10 -> nil
B. nil
C. 10 -> 20 -> 30 -> 40 -> nil
D. 40 -> 10 -> 20 -> 30 -> nil

Solution

  1. Step 1: First reversal

    Reversing 10->20->30->40->nil gives 40->30->20->10->nil.
  2. Step 2: Second reversal

    Reversing again restores original order: 10->20->30->40->nil.
  3. Final Answer:

    10 -> 20 -> 30 -> 40 -> nil -> Option C
  4. Quick Check:

    Double reversal returns original list [OK]
Hint: Reversing twice returns original list [OK]
Common Mistakes:
  • Assuming second reversal changes order differently
  • Confusing node values after reversal
  • Thinking list becomes empty
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