Bird
Raised Fist0
DSA Javaprogramming~20 mins

Reverse a Singly Linked List Recursive in DSA Java - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Recursive Reverse Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Reverse on a 3-Node List
What is the printed state of the linked list after recursively reversing the list 1 -> 2 -> 3 -> null?
DSA Java
class Node {
    int val;
    Node next;
    Node(int val) { this.val = val; this.next = null; }
}

public class Main {
    public static Node reverseRecursive(Node head) {
        if (head == null || head.next == null) return head;
        Node newHead = reverseRecursive(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.val + " -> ");
            head = head.next;
        }
        System.out.print("null");
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        head.next = new Node(2);
        head.next.next = new Node(3);
        head = reverseRecursive(head);
        printList(head);
    }
}
A1 -> 2 -> 3 -> null
B3 -> 2 -> 1 -> null
Cnull
D3 -> 1 -> 2 -> null
Attempts:
2 left
💡 Hint
Think about how the recursion reverses the links starting from the end.
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Reverse on Single Node List
What is the printed state of the linked list after recursively reversing the list 5 -> null?
DSA Java
class Node {
    int val;
    Node next;
    Node(int val) { this.val = val; this.next = null; }
}

public class Main {
    public static Node reverseRecursive(Node head) {
        if (head == null || head.next == null) return head;
        Node newHead = reverseRecursive(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.val + " -> ");
            head = head.next;
        }
        System.out.print("null");
    }

    public static void main(String[] args) {
        Node head = new Node(5);
        head = reverseRecursive(head);
        printList(head);
    }
}
A5 -> null
BError: NullPointerException
C5 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
A single node reversed is itself.
🔧 Debug
advanced
2:00remaining
Identify the Error in Recursive Reverse Implementation
What error will this code produce when trying to reverse a linked list recursively?
DSA Java
class Node {
    int val;
    Node next;
    Node(int val) { this.val = val; this.next = null; }
}

public class Main {
    public static Node reverseRecursive(Node head) {
        if (head == null || head.next == null) return head;
        head.next = head;
        Node newHead = reverseRecursive(head.next);
        head.next = null;
        return newHead;
    }
}
AStackOverflowError due to infinite recursion
BNullPointerException at head.next.next
CNo error, works correctly
DCompilation error due to missing return statement
Attempts:
2 left
💡 Hint
Check how the pointers are reassigned inside the recursion.
🧠 Conceptual
advanced
1:30remaining
Why is the Base Case Important in Recursive Reverse?
In the recursive reversal of a singly linked list, why do we check if head is null or head.next is null as the base case?
ATo skip the last node during reversal
BTo avoid NullPointerException when accessing head.next.next
CTo initialize the new head of the reversed list with the first node
DTo stop recursion when the list is empty or has only one node, which is already reversed
Attempts:
2 left
💡 Hint
Think about when recursion should stop.
❓ Predict Output
expert
2:30remaining
Output After Recursive Reverse on 4-Node List with Values 10, 20, 30, 40
What is the printed state of the linked list after recursively reversing the list 10 -> 20 -> 30 -> 40 -> null?
DSA Java
class Node {
    int val;
    Node next;
    Node(int val) { this.val = val; this.next = null; }
}

public class Main {
    public static Node reverseRecursive(Node head) {
        if (head == null || head.next == null) return head;
        Node newHead = reverseRecursive(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public static void printList(Node head) {
        while (head != null) {
            System.out.print(head.val + " -> ");
            head = head.next;
        }
        System.out.print("null");
    }

    public static void main(String[] args) {
        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(30);
        head.next.next.next = new Node(40);
        head = reverseRecursive(head);
        printList(head);
    }
}
AError: StackOverflowError
B10 -> 20 -> 30 -> 40 -> null
C40 -> 30 -> 20 -> 10 -> null
D40 -> 20 -> 30 -> 10 -> null
Attempts:
2 left
💡 Hint
Trace the recursion from the last node back to the first.

Practice

(1/5)
1. What is the main idea behind reversing a singly linked list using recursion?
easy
A. Reverse the rest of the list first, then fix the current node's next pointer
B. Swap the values of the first and last nodes only
C. Iterate through the list and reverse pointers in a loop
D. Create a new list and copy nodes in reverse order

Solution

  1. Step 1: Understand recursion on linked list

    Recursion works by solving a smaller part first, here reversing from the next node onward.
  2. Step 2: Fix current node after recursive call

    After reversing the rest, the current node's next node points back to it, reversing the link.
  3. Final Answer:

    Reverse the rest of the list first, then fix the current node's next pointer -> Option A
  4. Quick Check:

    Recursive reversal = Reverse rest then fix current [OK]
Hint: Think: reverse smaller list first, then fix current link [OK]
Common Mistakes:
  • Trying to swap node values instead of pointers
  • Reversing pointers before recursive call
  • Using iteration instead of recursion
2. Which of the following is the correct base case for a recursive function that reverses a singly linked list?
easy
A. if (head == null) return null.next;
B. if (head.next == null) return null;
C. if (head == null) return head.next;
D. if (head == null || head.next == null) return head;

Solution

  1. Step 1: Identify base case meaning

    The base case stops recursion when list is empty or has one node, so head is null or head.next is null.
  2. Step 2: Check each option

    if (head == null || head.next == null) return head; correctly returns head when list is empty or single node. Others cause errors or wrong returns.
  3. Final Answer:

    if (head == null || head.next == null) return head; -> Option D
  4. Quick Check:

    Base case = empty or single node returns head [OK]
Hint: Base case: empty or single node list returns head [OK]
Common Mistakes:
  • Returning null when list has one node
  • Accessing next of null causing errors
  • Missing base case leading to infinite recursion
3. What will be the output linked list after running this recursive reverse function on the list 1 -> 2 -> 3 -> null?
Node reverse(Node head) {
  if (head == null || head.next == null) return head;
  Node newHead = reverse(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}
medium
A. null
B. 1 -> 2 -> 3 -> null
C. 3 -> 2 -> 1 -> null
D. 3 -> 1 -> 2 -> null

Solution

  1. Step 1: Trace recursion on list 1->2->3

    Recursion goes to last node 3, then reverses links backward: 3 points to 2, 2 points to 1.
  2. Step 2: Final reversed list

    After fixing pointers, the list becomes 3 -> 2 -> 1 -> null.
  3. Final Answer:

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

    Reversed list = 3 -> 2 -> 1 [OK]
Hint: Last node becomes new head after recursion [OK]
Common Mistakes:
  • Not setting head.next to null causing cycle
  • Returning old head instead of new head
  • Confusing node values with pointers
4. Identify the error in this recursive reverse function and choose the fix:
Node reverse(Node head) {
  if (head == null) return null;
  Node newHead = reverse(head.next);
  head.next.next = head;
  return newHead;
}
medium
A. head.next.next = head; should be head.next = head.next.next;
B. Missing base case for single node; add 'head.next == null' check
C. Return statement should be 'return head;' instead of newHead
D. No error; code is correct

Solution

  1. Step 1: Check base case correctness

    Base case only checks head == null, missing single node case causes null pointer error at head.next.next.
  2. Step 2: Fix base case

    Adding 'head.next == null' to base case prevents error and correctly stops recursion.
  3. Final Answer:

    Missing base case for single node; add 'head.next == null' check -> Option B
  4. Quick Check:

    Base case must handle single node [OK]
Hint: Base case must check single node to avoid null pointer [OK]
Common Mistakes:
  • Ignoring single node base case causing runtime error
  • Incorrect pointer assignment causing infinite loop
  • Returning wrong node after recursion
5. Given a singly linked list with nodes 10 -> 20 -> 30 -> 40 -> null, you want to reverse it recursively but also count how many nodes were reversed. Which modification correctly adds counting nodes during recursion?
class Result {
  Node head;
  int count;
}

Result reverseCount(Node head) {
  if (head == null || head.next == null) {
    Result res = new Result();
    res.head = head;
    res.count = (head == null) ? 0 : 1;
    return res;
  }
  Result res = reverseCount(head.next);
  head.next.next = head;
  head.next = null;
  res.count += 1;
  return res;
}
hard
A. This code correctly reverses and counts nodes
B. Count should be incremented before recursive call
C. head.next = null; should be removed to avoid breaking list
D. Result class should store count as static variable

Solution

  1. Step 1: Analyze recursive counting logic

    Base case sets count to 1 or 0. Each recursive return increments count by 1 after reversing pointer.
  2. Step 2: Confirm correctness of pointer reversal and counting

    Pointer reversal is correct and count increments after recursion, so total nodes counted correctly.
  3. Final Answer:

    This code correctly reverses and counts nodes -> Option A
  4. Quick Check:

    Counting nodes during recursion works as coded [OK]
Hint: Increment count after recursion to count all nodes [OK]
Common Mistakes:
  • Incrementing count before recursion causing wrong totals
  • Removing head.next = null causing cycles
  • Using static variable causing shared state bugs
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