📖 Scenario: Imagine you have a chain of connected train cars, each linked to the next. You want to reverse the order of the cars so the last car becomes the first and the first becomes the last.
🎯 Goal: You will build a singly linked list and then reverse it using a recursive function. Finally, you will print the reversed list to see the new order.
📋 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: value -> value -> ... -> null
💡 Why This Matters
🌍 Real World
Reversing linked lists is useful in many applications like undo operations, navigation history, and data structure manipulation.
💼 Career
Understanding linked lists and recursion is important for software engineering interviews and working with low-level data structures.
Progress0 / 4 steps
1
Create the Singly Linked List
Create a class called ListNode with a constructor that takes value: number and next: ListNode | null. Then create three nodes with values 1, 2, and 3 linked together as head pointing to the first node.
DSA Typescript
Hint
Start by defining the ListNode class with value and next. Then create nodes from last to first linking them.
2
Add Recursive Reverse Function
Create a function called reverseList that takes head: ListNode | null and returns ListNode | null. Use recursion to reverse the linked list.
DSA Typescript
Hint
Use the base case when head is null or head.next is null. Then recursively reverse the rest and fix the links.
3
Reverse the Linked List
Call the reverseList function with head and store the result in a variable called reversedHead.
DSA Typescript
Hint
Simply call reverseList(head) and save the result in reversedHead.
4
Print the Reversed Linked List
Write a loop to print the values of the reversed linked list starting from reversedHead in the format: value -> value -> ... -> null.
DSA Typescript
Hint
Use a while loop to go through each node starting from reversedHead. Add each value and an arrow to a string, then add null at the end and print it.
Practice
(1/5)
1. What is the main idea behind reversing a singly linked list using recursion?
easy
A. Create a new list and copy nodes in reverse order
B. Swap the values of the first and last nodes only
C. Reverse the rest of the list first, then fix the current node's next pointer
D. Iterate through the list and reverse pointers using a loop
Solution
Step 1: Understand recursion on linked list
Recursion works by solving a smaller part first, here reversing the rest of the list beyond the current node.
Step 2: Fix current node's pointer after recursion
After the rest is reversed, 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 C
Recursion goes to node 3 (base case), then reverses links back: 3 points to 2, 2 points to 1.
Step 2: Update pointers to reverse list
head.next.next = head sets 2's next to 1, head.next = null breaks old link, final reversed list is 3 -> 2 -> 1 -> null.
Final Answer:
3 -> 2 -> 1 -> null -> Option A
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 cycles
Returning original head instead of new head
Confusing node values with pointers
4. Identify the bug in this recursive reverse function and choose the fix:
function reverseList(head: ListNode | null): ListNode | null {
if (!head) return null;
if (!head.next) return head;
const newHead = reverseList(head.next);
head.next = null;
head.next.next = head;
return newHead;
}
medium
A. Remove the base case for head.next === null
B. Swap the last two pointer assignments: set head.next.next before head.next = null
C. Return head instead of newHead
D. Add a loop to reverse pointers instead of recursion
Solution
Step 1: Analyze pointer assignments order
Setting head.next = null before head.next.next = head breaks the chain because head.next becomes null.
Step 2: Correct order to fix pointers
First set head.next.next = head to reverse link, then set head.next = null to break old link.
Final Answer:
Swap the last two pointer assignments: set head.next.next before head.next = null -> Option B
Quick Check:
Fix pointer order to avoid null errors [OK]
Hint: Set head.next.next before nullifying head.next [OK]
Common Mistakes:
Nullifying head.next too early
Ignoring base cases
Returning wrong node
5. Given a singly linked list with nodes containing values [10, 20, 30, 40, 50], which recursive approach correctly reverses the list and returns the new head?