Bird
Raised Fist0
DSA Javaprogramming

Reverse a Singly Linked List Iterative in DSA Java

Choose your learning style10 modes available
Mental Model
We change the direction of each link one by one so the list points backward instead of forward.
Analogy: Imagine a line of people holding hands forward. We ask each person to turn around and hold the hand of the person behind them instead.
head -> 1 -> 2 -> 3 -> null
prev = null
curr = 1
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> null
Goal: Reverse the list so it becomes 3 -> 2 -> 1 -> null
Step 1: Save next node after current (2), reverse current's pointer to prev (null), move prev to current (1), move current to saved next (2)
1 -> null [prev]
2 -> 3 -> null [curr]
Why: We start reversing the first node's pointer to null and prepare to move forward
Step 2: Save next node after current (3), reverse current's pointer to prev (1), move prev to current (2), move current to saved next (3)
2 -> 1 -> null [prev]
3 -> null [curr]
Why: We reverse the second node's pointer to point back to 1
Step 3: Save next node after current (null), reverse current's pointer to prev (2), move prev to current (3), move current to saved next (null)
3 -> 2 -> 1 -> null [prev]
null [curr]
Why: We reverse the last node's pointer and finish as current reaches null
Result:
3 -> 2 -> 1 -> null
Annotated Code
DSA Java
public class LinkedList {
    static class Node {
        int data;
        Node next;
        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    public static Node reverseIterative(Node head) {
        Node prev = null;
        Node curr = head;
        while (curr != null) {
            Node nextTemp = curr.next; // save next node
            curr.next = prev; // reverse pointer
            prev = curr; // move prev forward
            curr = nextTemp; // move curr forward
        }
        return prev; // new head
    }

    public static void printList(Node head) {
        Node curr = head;
        while (curr != null) {
            System.out.print(curr.data);
            if (curr.next != null) System.out.print(" -> ");
            curr = curr.next;
        }
        System.out.println(" -> null");
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);

        System.out.print("Original list: ");
        printList(head);

        head = reverseIterative(head);

        System.out.print("Reversed list: ");
        printList(head);
    }
}
Node nextTemp = curr.next; // save next node
store next node before changing pointer
curr.next = prev; // reverse pointer
reverse current node's pointer to previous node
prev = curr; // move prev forward
advance prev to current node
curr = nextTemp; // move curr forward
advance current to next node saved earlier
return prev; // new head
return new head after full reversal
OutputSuccess
Original list: 1 -> 2 -> 3 -> null Reversed list: 3 -> 2 -> 1 -> null
Complexity Analysis
Time: O(n) because we visit each node once to reverse pointers
Space: O(1) because we only use a few extra variables, no extra list
vs Alternative: Compared to recursive reversal which uses O(n) space on call stack, iterative is more space efficient
Edge Cases
empty list (head is null)
function returns null immediately, no reversal needed
DSA Java
while (curr != null) {
single node list
pointer reversal sets next to null but list remains same
DSA Java
curr.next = prev;
When to Use This Pattern
When asked to reverse a singly linked list efficiently, use the iterative pointer reversal pattern to change links in one pass.
Common Mistakes
Mistake: Not saving the next node before reversing pointer causes loss of access to rest of list
Fix: Always save curr.next in a temporary variable before changing curr.next
Mistake: Returning the original head instead of the new head after reversal
Fix: Return prev pointer after loop ends as it points to new head
Summary
Reverses the direction of a singly linked list by changing pointers iteratively.
Use when you need to reverse a linked list quickly without extra space.
The key is to save next node before reversing pointer to avoid losing the list.

Practice

(1/5)
1. What is the main purpose of reversing a singly linked list iteratively?
easy
A. To sort the list in ascending order
B. To delete all nodes from the list
C. To change the direction of all links so the last node becomes the first
D. To add a new node at the end of the list

Solution

  1. Step 1: Understand the linked list structure

    A singly linked list has nodes connected in one direction from head to tail.
  2. Step 2: Purpose of reversing

    Reversing changes the links so the last node points to the previous one, making it the new head.
  3. Final Answer:

    To change the direction of all links so the last node becomes the first -> Option C
  4. Quick Check:

    Reversing = changing link directions [OK]
Hint: Reversing flips the list direction, last becomes first [OK]
Common Mistakes:
  • Confusing reversing with sorting
  • Thinking reversing deletes nodes
  • Assuming reversing adds nodes
2. Which of the following is the correct way to initialize the previous pointer before starting the iterative reversal?
easy
A. Node prev = head;
B. Node prev = head.next;
C. Node prev = new Node();
D. Node prev = null;

Solution

  1. Step 1: Understand the role of previous pointer

    Previous pointer starts as null because the new tail's next should be null after reversal.
  2. Step 2: Initialization

    Setting prev to null ensures the first node processed points to null, ending the reversed list properly.
  3. Final Answer:

    Node prev = null; -> Option D
  4. Quick Check:

    Prev starts null to mark new tail [OK]
Hint: Prev pointer starts null to mark list end [OK]
Common Mistakes:
  • Initializing prev to head causes cycle
  • Creating new node wastes memory
  • Starting prev at head.next skips first node
3. What will be the output linked list after reversing the list 1 -> 2 -> 3 -> null iteratively?
medium
A. 1 -> 2 -> 3 -> null
B. 3 -> 2 -> 1 -> null
C. null -> 3 -> 2 -> 1
D. 2 -> 3 -> 1 -> null

Solution

  1. Step 1: Trace the reversal process

    Initially: 1 -> 2 -> 3 -> null. After reversal, links point backward: 3 -> 2 -> 1 -> null.
  2. Step 2: Confirm final head and tail

    Node 3 becomes new head, node 1 becomes tail pointing to null.
  3. Final Answer:

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

    Reversed list = 3 -> 2 -> 1 [OK]
Hint: Last node becomes first after reversal [OK]
Common Mistakes:
  • Assuming list stays same after reversal
  • Confusing null placement
  • Mixing node order
4. Identify the error in this iterative reversal code snippet:
Node prev = head;
Node current = head.next;
while (current != null) {
  Node nextNode = current.next;
  current.next = prev;
  prev = current;
  current = nextNode;
}
head.next = null;
head = prev;
medium
A. prev should be initialized to null, not head
B. current should start at head, not head.next
C. head.next should not be set to null
D. Loop condition should be current.next != null

Solution

  1. Step 1: Check initialization of prev pointer

    Prev must start as null to mark new tail's next as null after reversal.
  2. Step 2: Effect of wrong initialization

    Starting prev at head causes a cycle, breaking reversal logic.
  3. Final Answer:

    prev should be initialized to null, not head -> Option A
  4. Quick Check:

    Prev = null to avoid cycles [OK]
Hint: Prev must start null to avoid cycles [OK]
Common Mistakes:
  • Starting prev at head causes infinite loop
  • Wrong loop condition misses last node
  • Forgetting to set old head next to null
5. Given a singly linked list with nodes containing values 10 -> 20 -> 30 -> 40 -> null, which iterative code snippet correctly reverses it and prints the reversed list?
hard
A. Node prev = null; Node current = head; while (current != null) { Node next = current.next; current.next = prev; prev = current; current = next; } head = prev; // Print list starting from head
B. Node prev = head; Node current = head.next; while (current != null) { Node next = current.next; current.next = prev; prev = current; current = next; } head = prev; // Print list starting from head
C. Node current = head; while (current != null) { Node next = current.next; current.next = null; current = next; } // Print list starting from head
D. Node prev = null; Node current = head; while (current.next != null) { Node next = current.next; current.next = prev; prev = current; current = next; } head = prev; // Print list starting from head

Solution

  1. Step 1: Verify initialization of pointers

    Prev must start as null, current at head to reverse all nodes correctly.
  2. Step 2: Check loop condition and pointer updates

    Loop runs while current != null to process all nodes; inside loop, links reversed properly.
  3. Step 3: Confirm final head assignment and printing

    After loop, head is set to prev (new head). Printing from head shows reversed list.
  4. Final Answer:

    Option A code correctly reverses and prints the list -> Option A
  5. Quick Check:

    Prev=null, loop full list, head=prev [OK]
Hint: Prev null, loop full list, head = prev after reversal [OK]
Common Mistakes:
  • Starting prev at head causes cycle
  • Loop condition misses last node
  • Setting current.next = null inside loop breaks links
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