Bird
Raised Fist0
DSA C++programming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Recursive Reversal 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 calling the recursive reverse function on this list?

Initial list: 1 -> 2 -> 3 -> null
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* reverseRecursive(Node* head) {
    if (!head || !head->next) return head;
    Node* newHead = reverseRecursive(head->next);
    head->next->next = head;
    head->next = nullptr;
    return newHead;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head = reverseRecursive(head);
    printList(head);
    return 0;
}
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
C2 -> 3 -> 1 -> null
D3 -> 1 -> 2 -> null
Attempts:
2 left
💡 Hint
Think about how the recursion unwinds and reverses the links one by one.
❓ Predict Output
intermediate
2:00remaining
Output of Recursive Reverse on Single Node List
What is the printed state of the linked list after calling the recursive reverse function on a single node list?

Initial list: 5 -> null
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* reverseRecursive(Node* head) {
    if (!head || !head->next) return head;
    Node* newHead = reverseRecursive(head->next);
    head->next->next = head;
    head->next = nullptr;
    return newHead;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node(5);
    head = reverseRecursive(head);
    printList(head);
    return 0;
}
A5 -> 5 -> null
Bnull
C5 -> null
Dnull -> 5
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 recursive reverse function cause when called on a list with multiple nodes?
DSA C++
Node* reverseRecursive(Node* head) {
    if (!head) return nullptr;
    Node* newHead = reverseRecursive(head->next);
    head->next->next = head;
    head->next = nullptr;
    return newHead;
}
ACompilation error due to missing return statement
BNo error, works correctly
CInfinite recursion causing stack overflow
DRuntime error: dereferencing nullptr when head->next is nullptr
Attempts:
2 left
💡 Hint
Check the base case carefully and what happens when head->next is nullptr.
🧠 Conceptual
advanced
2:00remaining
Understanding Recursive Call Stack in List Reversal
During the recursive reversal of a singly linked list, what happens first when the recursion unwinds?
AThe last node becomes the new head and starts linking backwards
BThe first node is immediately set as the new head
CAll nodes are reversed simultaneously at the base case
DThe middle node is set as the new head
Attempts:
2 left
💡 Hint
Think about how recursion reaches the end before changing links.
❓ Predict Output
expert
3:00remaining
Output of Recursive Reverse on a 5-Node List with Duplicate Values
What is the printed state of the linked list after calling the recursive reverse function on this list?

Initial list: 4 -> 7 -> 7 -> 2 -> 4 -> null
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* reverseRecursive(Node* head) {
    if (!head || !head->next) return head;
    Node* newHead = reverseRecursive(head->next);
    head->next->next = head;
    head->next = nullptr;
    return newHead;
}

void printList(Node* head) {
    while (head) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
}

int main() {
    Node* head = new Node(4);
    head->next = new Node(7);
    head->next->next = new Node(7);
    head->next->next->next = new Node(2);
    head->next->next->next->next = new Node(4);
    head = reverseRecursive(head);
    printList(head);
    return 0;
}
A4 -> 7 -> 7 -> 2 -> 4 -> null
B4 -> 2 -> 7 -> 7 -> 4 -> null
C4 -> 7 -> 2 -> 7 -> 4 -> null
D4 -> 4 -> 7 -> 7 -> 2 -> null
Attempts:
2 left
💡 Hint
The reverse function reverses the entire list order, duplicates remain in reversed positions.

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 data 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 elements 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's pointer after recursion

    After reversing the rest, the current node's next node should point back to current node, then current node points to null.
  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 first [OK]
Hint: Think: reverse smaller list first, then fix current node [OK]
Common Mistakes:
  • Trying to swap data instead of pointers
  • Not setting current node's next to null
  • Confusing iterative and recursive methods
2. Which of the following is the correct base case for a recursive function that reverses a singly linked list?
easy
A. if (head->next == nullptr) return head;
B. if (head == nullptr || head->next == nullptr) return head;
C. if (head == nullptr) return nullptr;
D. if (head->next == nullptr) head = nullptr;

Solution

  1. Step 1: Identify base cases for recursion

    Base case should handle empty list and single node list, both are already reversed.
  2. Step 2: Choose condition covering both cases

    Condition 'head == nullptr || head->next == nullptr' returns head for empty or single node list.
  3. Final Answer:

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

    Base case = empty or single node [OK]
Hint: Base case covers empty or single node list [OK]
Common Mistakes:
  • Returning nullptr for single node list
  • Not handling empty list
  • Incorrectly modifying head in base case
3. Given the following linked list: 1 -> 2 -> 3 -> null, what will be the printed output after applying this recursive reverse function?
Node* reverse(Node* head) {
  if (!head || !head->next) return head;
  Node* newHead = reverse(head->next);
  head->next->next = head;
  head->next = nullptr;
  return newHead;
}
medium
A. 3 -> 1 -> 2 -> null
B. 1 -> 2 -> 3 -> null
C. 2 -> 3 -> 1 -> null
D. 3 -> 2 -> 1 -> null

Solution

  1. Step 1: Trace recursion calls

    reverse(1) calls reverse(2), which calls reverse(3). reverse(3) returns node 3 as base case.
  2. Step 2: Reverse pointers on return

    On unwind from reverse(3), in reverse(2): head(2)->next(3)->next = 2 so 3->next=2, then 2->next=nullptr. Then in reverse(1): 2->next=1, 1->next=nullptr.
  3. Final Answer:

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

    Reversed list = 3->2->1 [OK]
Hint: Follow recursion unwind to see pointer reversal [OK]
Common Mistakes:
  • Not setting head->next to nullptr causing cycle
  • Confusing node values order
  • Returning wrong node as new head
4. Identify the bug in this recursive reverse function and select the correct fix:
Node* reverse(Node* head) {
  if (!head || !head->next) return head;
  Node* newHead = reverse(head->next);
  head->next->next = head;
  return newHead;
}
medium
A. Remove the recursive call and use iteration
B. Change base case to 'if (head->next == nullptr) return head;'
C. Add 'head->next = nullptr;' before returning newHead
D. Return 'head' instead of 'newHead'

Solution

  1. Step 1: Analyze missing pointer update

    Function misses setting 'head->next = nullptr', causing cycle in reversed list.
  2. Step 2: Fix by setting 'head->next = nullptr'

    Adding 'head->next = nullptr;' breaks old link, preventing cycle and correctly terminates list.
  3. Final Answer:

    Add 'head->next = nullptr;' before returning newHead -> Option C
  4. Quick Check:

    Break old next link to avoid cycle [OK]
Hint: Always set current node's next to nullptr after reversing [OK]
Common Mistakes:
  • Forgetting to break old next pointer
  • Incorrect base case causing null pointer dereference
  • Returning wrong node as new head
5. You want to reverse a singly linked list recursively but also count how many nodes were reversed. Which modification correctly adds counting without breaking the reversal?
Node* reverseCount(Node* head, int &count) {
  if (!head || !head->next) {
    count++;
    return head;
  }
  Node* newHead = reverseCount(head->next, count);
  head->next->next = head;
  head->next = nullptr;
  count++;
  return newHead;
}
What will be the value of count after reversing a list with 4 nodes?
hard
A. 4
B. 3
C. 5
D. 0

Solution

  1. Step 1: Understand counting in base case

    When recursion reaches last node, count increments by 1 for that node.
  2. Step 2: Count increments on each return step

    Each return from recursion increments count by 1 for each node processed, total nodes counted equals list length.
  3. Final Answer:

    4 -> Option A
  4. Quick Check:

    Count increments once per node [OK]
Hint: Increment count once per node during recursion [OK]
Common Mistakes:
  • Counting only in base case missing other nodes
  • Incrementing count twice per node
  • Not passing count by reference
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