📖 Scenario: You are working on a simple contact list app. The contacts are stored in a singly linked list, where each node holds a contact's name. You want to reverse the order of contacts using a recursive method.
🎯 Goal: Build a singly linked list of contacts and write a recursive function to reverse the list. Finally, print the reversed list to see the new order.
📋 What You'll Learn
Create a singly linked list with exactly these contacts in order: "Alice", "Bob", "Charlie", "Diana"
Add a recursive method called reverseList that reverses the linked list
Use a helper method or variable if needed to manage recursion
Print the reversed linked list showing each contact's name separated by arrows (e.g., Alice -> Bob -> null)
💡 Why This Matters
🌍 Real World
Reversing linked lists is useful in many applications like undo operations, reversing order of tasks, or processing data backwards.
💼 Career
Understanding recursive linked list reversal is a common interview question and helps build strong problem-solving skills in data structures.
Progress0 / 4 steps
1
Create the Singly Linked List
Create a class called Node with a String name and a Node next. Then create a linked list with four nodes in this order: Alice, Bob, Charlie, Diana. Store the first node in a variable called head.
DSA Java
Hint
Define the Node class with name and next. Then link nodes in order.
2
Add Recursive Reverse Method Signature
Inside the Main class, add a static method called reverseList that takes a Node head parameter and returns a Node. Do not write the method body yet, just the method signature and an empty body.
DSA Java
Hint
Write the method header for reverseList that takes Node head and returns Node.
3
Implement Recursive Logic to Reverse the List
Complete the reverseList method to reverse the linked list recursively. Use these rules: if head is null or head.next is null, return head. Otherwise, recursively reverse the rest of the list, then set head.next.next = head and head.next = null. Finally, return the new head.
DSA Java
Hint
Use the base case for empty or single node. Then reverse the rest and fix pointers.
4
Print the Reversed Linked List
In the main method, call reverseList(head) and store the result in head. Then print the reversed list by iterating from head and printing each name followed by -> . End with null.
DSA Java
Hint
Call reverseList and then loop through the list to print each name followed by -> . End with null.
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
Step 1: Understand recursion on linked list
Recursion works by solving a smaller part first, here reversing from the next node onward.
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.
Final Answer:
Reverse the rest of the list first, then fix the current node's next pointer -> Option A
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
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.
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.
Final Answer:
if (head == null || head.next == null) return head; -> Option D
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?
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
Step 1: Analyze recursive counting logic
Base case sets count to 1 or 0. Each recursive return increments count by 1 after reversing pointer.
Step 2: Confirm correctness of pointer reversal and counting
Pointer reversal is correct and count increments after recursion, so total nodes counted correctly.
Final Answer:
This code correctly reverses and counts nodes -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.