Bird
Raised Fist0
DSA Javascriptprogramming~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Linked List Reversal
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 will be the printed state after running the iterative reverse function?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

// Create linked list 1 -> 2 -> 3 -> null
const head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);

const reversedHead = reverseList(head);

// Function to print list
function printList(node) {
  let result = '';
  while (node !== null) {
    result += node.value + ' -> ';
    node = node.next;
  }
  result += 'null';
  console.log(result);
}

printList(reversedHead);
A3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> null
Cnull
D1 -> null
Attempts:
2 left
💡 Hint
Think about how the pointers change direction in each iteration.
❓ Predict Output
intermediate
2:00remaining
What is the output after reversing a single-node linked list?
Given a linked list with only one node: 5 -> null, what will be the printed state after running the iterative reverse function?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

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

function printList(node) {
  let result = '';
  while (node !== null) {
    result += node.value + ' -> ';
    node = node.next;
  }
  result += 'null';
  console.log(result);
}

printList(reversedHead);
AError: Cannot read property 'next' of null
Bnull
C5 -> 5 -> null
D5 -> null
Attempts:
2 left
💡 Hint
Reversing a single node list should not change the list.
❓ Predict Output
advanced
2:00remaining
What is the output after reversing this longer linked list iteratively?
Given the linked list 10 -> 20 -> 30 -> 40 -> null, what will be the printed state after running the iterative reverse function?
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);

const reversedHead = reverseList(head);

function printList(node) {
  let result = '';
  while (node !== null) {
    result += node.value + ' -> ';
    node = node.next;
  }
  result += 'null';
  console.log(result);
}

printList(reversedHead);
A40 -> 30 -> 20 -> 10 -> null
B10 -> 20 -> 30 -> 40 -> null
Cnull
D40 -> 30 -> 20 -> null
Attempts:
2 left
💡 Hint
The last node becomes the new head after reversal.
❓ Predict Output
advanced
2:00remaining
What is the output after reversing an empty linked list?
Given an empty linked list (head is null), what will be the printed state after running the iterative reverse function?
DSA Javascript
function reverseList(head) {
  let prev = null;
  let current = head;
  while (current !== null) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}

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

function printList(node) {
  let result = '';
  while (node !== null) {
    result += node.value + ' -> ';
    node = node.next;
  }
  result += 'null';
  console.log(result);
}

printList(reversedHead);
AError: Cannot read property 'next' of null
Bnull
CEmpty list
D0 -> null
Attempts:
2 left
💡 Hint
Reversing an empty list should return empty.
🧠 Conceptual
expert
2:00remaining
Which statement about iterative reversal of singly linked lists is true?
Consider the iterative method to reverse a singly linked list. Which of the following statements is correct?
AThe iterative reversal requires extra space proportional to the list size.
BThe iterative reversal cannot be done without recursion.
CThe iterative reversal changes the next pointers of nodes to point backwards, using constant extra space.
DThe iterative reversal keeps the original head node as the head after reversal.
Attempts:
2 left
💡 Hint
Think about how pointers are changed during iteration.

Practice

(1/5)
1. What does reversing a singly linked list iteratively mean?
head -> 1 -> 2 -> 3 -> null becomes?
easy
A. head -> 3 -> 2 -> 1 -> null
B. head -> 1 -> 2 -> 3 -> null
C. head -> null -> 1 -> 2 -> 3
D. head -> 2 -> 3 -> 1 -> null

Solution

  1. Step 1: Understand the linked list order

    The original list goes from 1 to 3 in order: 1 -> 2 -> 3 -> null.
  2. Step 2: Reverse the pointers iteratively

    Reversing means the last node becomes first, so the list becomes 3 -> 2 -> 1 -> null.
  3. Final Answer:

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

    Reverse order = head -> 3 -> 2 -> 1 -> null [OK]
Hint: Reversing flips the list order completely [OK]
Common Mistakes:
  • Thinking the list stays the same after reverse
  • Confusing node values with pointers
  • Assuming null moves to front
2. Which of the following is the correct way to initialize pointers for iterative reversal of a singly linked list in JavaScript?
easy
A. let prev = null, current = null;
B. let prev = head, current = null;
C. let prev = head, current = head.next;
D. let prev = null, current = head;

Solution

  1. Step 1: Initialize previous pointer to null

    Before reversal, previous node is null because head has no previous.
  2. Step 2: Initialize current pointer to head

    Current starts at head to traverse the list.
  3. Final Answer:

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

    Start prev=null, current=head [OK]
Hint: Prev starts null, current starts at head [OK]
Common Mistakes:
  • Setting prev to head initially
  • Setting current to null at start
  • Swapping initialization order
3. What is the output of the following code?
class Node {
  constructor(val) {
    this.val = val;
    this.next = null;
  }
}

function reverseList(head) {
  let prev = null;
  let current = head;
  while (current) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  return prev;
}

// Create list: 1 -> 2 -> 3 -> null
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(3);

let reversedHead = reverseList(head);

let result = [];
let node = reversedHead;
while (node) {
  result.push(node.val);
  node = node.next;
}
console.log(result);
medium
A. []
B. [3, 2, 1]
C. [null]
D. [1, 2, 3]

Solution

  1. Step 1: Trace reversal loop

    Each iteration reverses the next pointer: 1->2->3 becomes 3->2->1.
  2. Step 2: Collect values from reversed list

    After reversal, traversing from prev (new head) gives values 3, 2, 1.
  3. Final Answer:

    [3, 2, 1] -> Option B
  4. Quick Check:

    Reversed list values = [3, 2, 1] [OK]
Hint: Reversed list values appear in reverse order [OK]
Common Mistakes:
  • Returning original head instead of prev
  • Not updating current pointer correctly
  • Collecting values before reversal
4. Find the error in this iterative reversal code snippet:
function reverseList(head) {
  let prev = head;
  let current = head.next;
  while (current !== null) {
    let nextTemp = current.next;
    current.next = prev;
    prev = current;
    current = nextTemp;
  }
  head.next = null;
  return prev;
}
medium
A. head.next should not be set to null
B. current should start as head, not head.next
C. prev should start as null, not head
D. nextTemp should be assigned after changing current.next

Solution

  1. Step 1: Check initial pointer setup

    prev must start as null because head has no previous node before reversal.
  2. Step 2: Understand impact of wrong prev initialization

    Starting prev as head causes a cycle or incorrect links during reversal.
  3. Final Answer:

    prev should start as null, not head -> Option C
  4. Quick Check:

    prev=null start fixes reversal [OK]
Hint: Prev must start null to avoid cycles [OK]
Common Mistakes:
  • Starting prev at head causing loops
  • Not setting head.next to null after reversal
  • Changing nextTemp after modifying current.next
5. Given a singly linked list with nodes containing values 1 -> 2 -> 3 -> 4 -> 5 -> null, write an iterative function to reverse only the first 3 nodes. What will be the list after reversal?
hard
A. 3 -> 2 -> 1 -> 4 -> 5 -> null
B. 3 -> 2 -> 1 -> null
C. 5 -> 4 -> 3 -> 2 -> 1 -> null
D. 1 -> 2 -> 3 -> 4 -> 5 -> null

Solution

  1. Step 1: Reverse first 3 nodes iteratively

    Reverse nodes 1, 2, 3 to get 3 -> 2 -> 1.
  2. Step 2: Connect reversed part to remaining list

    Node 1 (now last in reversed part) points to node 4, preserving rest of list.
  3. Final Answer:

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

    Partial reversal keeps rest intact [OK]
Hint: Reverse only first k nodes, link rest after [OK]
Common Mistakes:
  • Reversing entire list instead of first 3 nodes
  • Not reconnecting reversed part to rest
  • Breaking list by losing tail connections
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