Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Reverse a Singly Linked List Recursive in DSA Typescript - 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 Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}

// Create list: 1 -> 2 -> 3 -> null
const node3 = new ListNode(3);
const node2 = new ListNode(2, node3);
const node1 = new ListNode(1, node2);

const reversedHead = reverseList(node1);

// Function to print list
function printList(head: ListNode | null): string {
  let current = head;
  let result = '';
  while (current) {
    result += current.val + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(reversedHead));
A3 -> 1 -> 2 -> null
B1 -> 2 -> 3 -> null
Cnull
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Think about how recursion reverses the links starting from the end of the list.
❓ Predict Output
intermediate
2:00remaining
Output When Reversing a Single Node List
What is the printed state of the linked list after recursively reversing the list 5 -> null?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}

const node = new ListNode(5);
const reversedHead = reverseList(node);

function printList(head: ListNode | null): string {
  let current = head;
  let result = '';
  while (current) {
    result += current.val + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(reversedHead));
Anull
B5 -> 5 -> null
C5 -> null
DError: Cannot read property 'next' of null
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 reversing the list 1 -> 2 -> 3 -> null?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseList(head.next);
  head.next = head;
  head.next = null;
  return newHead;
}

const node3 = new ListNode(3);
const node2 = new ListNode(2, node3);
const node1 = new ListNode(1, node2);

const reversedHead = reverseList(node1);

function printList(head: ListNode | null): string {
  let current = head;
  let result = '';
  while (current) {
    result += current.val + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(reversedHead));
ATypeError: Cannot set property 'next' of undefined
BStack overflow due to infinite recursion
C1 -> null
D3 -> 2 -> 1 -> null
Attempts:
2 left
💡 Hint
Check how the next pointers are reassigned after recursion.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Reverse on a 4-Node List with Duplicate Values
What is the printed state of the linked list after recursively reversing the list 4 -> 2 -> 2 -> 1 -> null?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val?: number, next?: ListNode | null) {
    this.val = val === undefined ? 0 : val;
    this.next = next === undefined ? null : next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}

const node4 = new ListNode(1);
const node3 = new ListNode(2, node4);
const node2 = new ListNode(2, node3);
const node1 = new ListNode(4, node2);

const reversedHead = reverseList(node1);

function printList(head: ListNode | null): string {
  let current = head;
  let result = '';
  while (current) {
    result += current.val + ' -> ';
    current = current.next;
  }
  result += 'null';
  return result;
}

console.log(printList(reversedHead));
A1 -> 2 -> 2 -> 4 -> null
B1 -> 4 -> 2 -> 2 -> null
C4 -> 2 -> 2 -> 1 -> null
D2 -> 2 -> 1 -> 4 -> null
Attempts:
2 left
💡 Hint
The reversal changes the order but keeps all nodes intact.
🧠 Conceptual
expert
2:00remaining
Why Does Recursive Reverse Use head.next.next = head?
In the recursive function to reverse a singly linked list, why do we assign 'head.next.next = head' instead of 'head.next = head'?
ABecause 'head.next = head' is invalid syntax and causes a compile error.
BBecause 'head.next.next = head' reverses the link by pointing the next node back to the current node, while 'head.next = head' creates a cycle.
CBecause 'head.next.next = head' sets the head to null, ending the list correctly.
DBecause 'head.next = head' deletes the next node, losing part of the list.
Attempts:
2 left
💡 Hint
Think about how links between nodes are reversed step-by-step.

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

  1. 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.
  2. 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.
  3. Final Answer:

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

    Recursive reversal = reverse rest + fix pointer [OK]
Hint: Think: reverse smaller list first, then fix current link [OK]
Common Mistakes:
  • Trying to swap node values instead of pointers
  • Not fixing the current node's next pointer after recursion
  • 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 head.next;
B. if (head.next === null) return null;
C. if (head === null) return head.next.next;
D. if (head === null || head.next === null) return head;

Solution

  1. Step 1: Identify when recursion should stop

    The base case is when the list is empty or has only one node, so no reversal needed.
  2. Step 2: Check the correct base case syntax

    Return the head if it is null or its next is null, meaning end or single node.
  3. Final Answer:

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

    Base case = empty or single node [OK]
Hint: Base case: empty or single node returns itself [OK]
Common Mistakes:
  • Returning null when only one node exists
  • Accessing head.next without checking if head is null
  • Incorrectly returning head.next or head.next.next
3. What will be the output linked list after running this recursive reverse function on the list 1 -> 2 -> 3 -> null?
function reverseList(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}
medium
A. 3 -> 2 -> 1 -> null
B. 1 -> 2 -> 3 -> null
C. null
D. 2 -> 3 -> 1 -> null

Solution

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

    Recursion goes to node 3 (base case), then reverses links back: 3 points to 2, 2 points to 1.
  2. 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.
  3. Final Answer:

    3 -> 2 -> 1 -> null -> Option A
  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 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

  1. Step 1: Analyze pointer assignments order

    Setting head.next = null before head.next.next = head breaks the chain because head.next becomes null.
  2. 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.
  3. Final Answer:

    Swap the last two pointer assignments: set head.next.next before head.next = null -> Option B
  4. 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?
function reverseList(head: ListNode | null): ListNode | null {
  if (!head || !head.next) return head;
  const newHead = reverseList(head.next);
  head.next.next = head;
  head.next = null;
  return newHead;
}
What will be the final linked list after calling reverseList on the head node with value 10?
hard
A. 50 -> 40 -> 30 -> 20 -> 10 -> null
B. 10 -> 20 -> 30 -> 40 -> 50 -> null
C. null
D. 20 -> 30 -> 40 -> 50 -> 10 -> null

Solution

  1. Step 1: Understand recursive reversal on list [10,20,30,40,50]

    The recursion reaches the last node (50), then reverses links step-by-step back to the first node.
  2. Step 2: Trace pointer changes during recursion unwind

    Each node's next node points back to it, and its next pointer is set to null, resulting in reversed list 50 -> 40 -> 30 -> 20 -> 10 -> null.
  3. Final Answer:

    50 -> 40 -> 30 -> 20 -> 10 -> null -> Option A
  4. Quick Check:

    Recursive reverse flips list order [OK]
Hint: Last node becomes new head, links reversed back [OK]
Common Mistakes:
  • Expecting original order after reversal
  • Not setting head.next to null causing cycles
  • Confusing node values with pointers
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