📖 Scenario: You are working on a simple contact list app. The contacts are stored in a singly linked list, where each contact points to the next one. You want to reverse the order of contacts using a recursive method, so the last contact becomes the first.
🎯 Goal: Build a recursive function to reverse a singly linked list and print the reversed list.
📋 What You'll Learn
Create a singly linked list with exact nodes and values
Add a recursive function to reverse the linked list
Print the reversed linked list in the format: value1 -> value2 -> ... -> null
💡 Why This Matters
🌍 Real World
Linked lists are used in many apps to manage ordered data like playlists, contact lists, or undo history. Reversing a list is a common operation to change the order.
💼 Career
Understanding linked lists and recursion is important for coding interviews and working with low-level data structures in software development.
Progress0 / 4 steps
1
Create the Singly Linked List
Create a class called Node with a constructor that takes value and sets this.value and this.next to null. Then create three nodes with values 10, 20, and 30. Link them so that node1.next = node2 and node2.next = node3. Finally, create a variable called head that points to node1.
DSA Javascript
Hint
Define a Node class with value and next properties. Create three nodes and link them in order.
2
Add Recursive Reverse Function
Create a function called reverseRecursive that takes a parameter node. If node is null or node.next is null, return node. Otherwise, call reverseRecursive(node.next) and store the result in newHead. Then set node.next.next = node and node.next = null. Finally, return newHead.
DSA Javascript
Hint
Use recursion to reach the last node, then reverse the links on the way back.
3
Reverse the List Using the Recursive Function
Create a variable called reversedHead and set it to the result of calling reverseRecursive(head).
DSA Javascript
Hint
Call the recursive function with the original head and save the result.
4
Print the Reversed Linked List
Write a function called printList that takes a parameter node. It should print the values of the linked list nodes separated by -> and end with null. Then call printList(reversedHead).
DSA Javascript
Hint
Traverse the list from the new head and build a string with arrows, then print it.
Practice
(1/5)
1. What is the main idea behind reversing a singly linked list using recursion?
easy
A. Swap the values of the first and last nodes only
B. Reverse the rest of the list first, then fix the current node's next pointer
C. Iterate through the list using a loop and reverse pointers
D. Create a new list and copy nodes in reverse order
Solution
Step 1: Understand recursion on linked list
Recursion reverses the list by first reversing the smaller list after the current node.
Step 2: Fix the current node's next pointer
After recursion returns, the current node's next node points back to the current node, reversing the link.
Final Answer:
Reverse the rest of the list first, then fix the current node's next pointer -> Option B
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
Using iteration instead of recursion
Not fixing the current node's next pointer after 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 head;
B. if (head.next.next === null) return head.next;
C. if (head.next === null) return head;
D. if (head === null || head.next === null) return head;
Solution
Step 1: Identify base cases for recursion
The base case must handle empty list (null) and single node list (next is null).
Step 2: Combine conditions for base case
Return head if it is null or if it is the last node (head.next is null).
Final Answer:
if (head === null || head.next === null) return head; -> Option D
Quick Check:
Base case = empty or single node list [OK]
Hint: Base case covers empty or single node list [OK]
Common Mistakes:
Using only null check without single node check
Returning wrong node in base case
Checking head.next.next which may cause error if head.next is null
3. What will be the output linked list after running this recursive reverse function on the list 1 -> 2 -> 3 -> null?
A. head.next = head; creates a cycle, should be head.next.next = head;
B. Base case is missing for empty list
C. Function does not return newHead
D. head.next is set to null before recursion
Solution
Step 1: Analyze pointer assignment
head.next = head sets next pointer of current node to itself, causing cycle.
Step 2: Correct pointer fix
It should be head.next.next = head to reverse the link properly.
Final Answer:
head.next = head; creates a cycle, should be head.next.next = head; -> Option A
Quick Check:
Pointer fix must be head.next.next = head [OK]
Hint: Fix pointer with head.next.next = head, not head.next = head [OK]
Common Mistakes:
Assigning head.next to head causing infinite loop
Forgetting to set head.next to null
Incorrect base case handling
5. Given a singly linked list with nodes containing values [10, 20, 30, 40, 50], how would you modify the recursive reverse function to also count the number of nodes reversed and return it along with the new head?
hard
A. Return an object {newHead, count} from recursion and increment count on unwind
B. Use a global variable to count nodes during recursion
C. Count nodes before recursion and return only new head
D. Modify nodes to store count and update during recursion
Solution
Step 1: Change recursion to return both head and count
Return an object with newHead and count from each recursive call.
Step 2: Increment count on each unwind step
When recursion returns, add 1 to count to track nodes reversed.
Final Answer:
Return an object {newHead, count} from recursion and increment count on unwind -> Option A
Quick Check:
Return combined result object with count [OK]
Hint: Return object with head and count, increment count on return [OK]
Common Mistakes:
Using global variables causing side effects
Counting nodes before reversal, not during recursion
Modifying node structure unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.