Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Reverse a Singly Linked List Iterative in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Reversal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of reversing this linked list iteratively?
Given the linked list 1 -> 2 -> 3 -> null, what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val: number, next: ListNode | null = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;
  while (current !== null) {
    const nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}

// Create linked list 1 -> 2 -> 3 -> null
const head = new ListNode(1, new ListNode(2, new ListNode(3)));
const reversedHead = reverseList(head);

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

console.log(printList(reversedHead));
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
C1 -> null
Dnull
Attempts:
2 left
💡 Hint
Think about how the pointers change direction step by step.
❓ Predict Output
intermediate
1:30remaining
What is the output after reversing a single-node linked list?
Given a linked list with a single node 5 -> null, what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val: number, next: ListNode | null = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;
  while (current !== null) {
    const nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}

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

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

console.log(printList(reversedHead));
Anull -> 5
B5 -> null
C5 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
🔧 Debug
advanced
2:00remaining
Identify the error in this iterative reverse function
What error will this code produce when reversing a linked list?
DSA Typescript
function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;
  while (current !== null) {
    const nextTemp = current.next;
    current.next = current;
    prev = current;
    current = nextTemp;
  }
  return prev;
}
ASyntaxError: Unexpected token
BTypeError: Cannot read property 'next' of null
CInfinite loop due to self-pointing node
DReturns reversed list correctly
Attempts:
2 left
💡 Hint
Look at how the next pointer is assigned inside the loop.
❓ Predict Output
advanced
1:30remaining
What is the output after reversing an empty linked list?
Given an empty linked list (head is null), what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val: number, next: ListNode | null = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;
  while (current !== null) {
    const nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}

const head = null;
const reversedHead = reverseList(head);

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

console.log(printList(reversedHead));
Anull
B0 -> null
Cundefined
DThrows TypeError
Attempts:
2 left
💡 Hint
Reversing an empty list should return empty.
❓ Predict Output
expert
2:30remaining
What is the output after reversing this linked list with duplicate values?
Given the linked list 4 -> 4 -> 5 -> 6 -> null, what is the printed state after running the iterative reverse function?
DSA Typescript
class ListNode {
  val: number;
  next: ListNode | null;
  constructor(val: number, next: ListNode | null = null) {
    this.val = val;
    this.next = next;
  }
}

function reverseList(head: ListNode | null): ListNode | null {
  let prev: ListNode | null = null;
  let current = head;
  while (current !== null) {
    const nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}

const head = new ListNode(4, new ListNode(4, new ListNode(5, new ListNode(6))));
const reversedHead = reverseList(head);

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

console.log(printList(reversedHead));
A6 -> 5 -> 4 -> null
Bnull
C4 -> 4 -> 5 -> 6 -> null
D6 -> 5 -> 4 -> 4 -> null
Attempts:
2 left
💡 Hint
The reversal should keep all nodes but reverse their order.

Practice

(1/5)
1. What is the main purpose of reversing a singly linked list iteratively?
easy
A. To sort the linked list in ascending order
B. To change the direction of the links between nodes without using recursion
C. To delete all nodes from the linked list
D. To find the middle node of the linked list

Solution

  1. Step 1: Understand the operation of reversing

    Reversing a singly linked list means changing the direction of the next pointers so the last node becomes the first.
  2. Step 2: Identify iterative approach

    The iterative method changes links step-by-step using a loop without recursion.
  3. Final Answer:

    To change the direction of the links between nodes without using recursion -> Option B
  4. Quick Check:

    Reversing iteratively = changing links direction without recursion [OK]
Hint: Reversing means flipping links, not sorting or deleting [OK]
Common Mistakes:
  • Confusing reversing with sorting
  • Thinking recursion is required
  • Assuming nodes are deleted
2. Which of the following TypeScript code snippets correctly initializes pointers for iterative reversal of a singly linked list?
easy
A. let prev = null; let current = head;
B. let prev = head; let current = null;
C. let prev = head; let current = head.next;
D. let prev = null; let current = null;

Solution

  1. Step 1: Understand pointer roles

    Prev starts as null because the new tail points to null; current starts at head to traverse.
  2. Step 2: Match initialization

    Only let prev = null; let current = head; sets prev to null and current to head correctly for iteration.
  3. Final Answer:

    let prev = null; let current = head; -> Option A
  4. Quick Check:

    Prev null, current head = correct start [OK]
Hint: Prev starts null, current starts at head for iteration [OK]
Common Mistakes:
  • Setting prev to head initially
  • Starting current as null
  • Both pointers null at start
3. Given the linked list: 1 -> 2 -> 3 -> null, what will be the output after running this iterative reversal code?
function reverseList(head: ListNode | null): ListNode | null {
  let prev = null;
  let current = head;
  while (current !== null) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}
medium
A. null
B. 1 -> 2 -> 3 -> null
C. 3 -> 2 -> 1 -> null
D. 1 -> null

Solution

  1. Step 1: Trace the reversal loop

    Initially prev=null, current=1. Each iteration reverses current.next to prev and moves pointers forward.
  2. Step 2: Final reversed list

    After loop, prev points to node 3, which links to 2, then 1, then null.
  3. Final Answer:

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

    Reversed list = 3 -> 2 -> 1 -> null [OK]
Hint: Follow pointers step-by-step to see reversed links [OK]
Common Mistakes:
  • Returning head instead of prev
  • Not updating current correctly
  • Confusing next pointer updates
4. Identify the error in this iterative reversal code snippet:
function reverseList(head: ListNode | null): ListNode | null {
  let prev = head;
  let current = head.next;
  while (current !== null) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}
medium
A. current should start at head, not head.next
B. The function should return current instead of prev
C. nextTemp should be assigned after updating current.next
D. prev should be initialized to null, not head

Solution

  1. Step 1: Check pointer initialization

    Prev must start as null because the new tail points to null, not head.
  2. Step 2: Understand impact of wrong initialization

    Starting prev at head causes a cycle or incorrect reversal.
  3. Final Answer:

    prev should be initialized to null, not head -> Option D
  4. Quick Check:

    Prev must be null at start [OK]
Hint: Prev always null at start to avoid cycles [OK]
Common Mistakes:
  • Starting prev at head causing loops
  • Starting current at wrong node
  • Returning wrong pointer
5. You have a singly linked list with nodes containing values: 10 -> 20 -> 30 -> 40 -> null. After reversing it iteratively, you want to insert a new node with value 25 between 20 and 30 in the reversed list. What will be the correct order of nodes after insertion?
hard
A. 40 -> 30 -> 25 -> 20 -> 10 -> null
B. 40 -> 30 -> 20 -> 25 -> 10 -> null
C. 40 -> 30 -> 20 -> 10 -> 25 -> null
D. 40 -> 25 -> 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Reverse the original list

    Original: 10 -> 20 -> 30 -> 40 -> null reversed becomes 40 -> 30 -> 20 -> 10 -> null.
  2. Step 2: Insert 25 between 30 and 20

    In reversed list, 30 points to 20, so insert 25 after 30: 30 -> 25 -> 20.
  3. Step 3: Final list order

    40 -> 30 -> 25 -> 20 -> 10 -> null.
  4. Final Answer:

    40 -> 30 -> 25 -> 20 -> 10 -> null -> Option A
  5. Quick Check:

    Insert between 30 and 20 in reversed list = 40->30->25->20->10 [OK]
Hint: Reverse first, then insert at correct reversed position [OK]
Common Mistakes:
  • Inserting 25 between wrong nodes after reversal
  • Confusing original and reversed order
  • Not updating next pointers correctly
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