📖 Scenario: You are working on a simple program that manages a list of tasks. The tasks are stored in a singly linked list, where each task points to the next one. Sometimes, you want to see the tasks in reverse order.
🎯 Goal: Build a program that creates a singly linked list of tasks, then reverses the list using an iterative method, and finally prints the reversed list.
📋 What You'll Learn
Create a singly linked list with exactly three nodes containing the values 'Task1', 'Task2', and 'Task3' in that order.
Create a variable called current to help traverse the list.
Write an iterative function called reverseList that reverses the singly linked list.
Print the reversed linked list values separated by ' -> ' and ending with ' -> null'.
💡 Why This Matters
🌍 Real World
Reversing linked lists is useful in many applications like undo operations, navigation history, and data processing pipelines where order reversal is needed.
💼 Career
Understanding linked list reversal is a common interview question and helps build strong problem-solving skills for software development roles.
Progress0 / 4 steps
1
Create the singly linked list
Create a class called ListNode with a constructor that takes a value of type string and a next pointer initialized to null. Then create three nodes with values 'Task1', 'Task2', and 'Task3' linked in that order. Finally, create a variable called head that points to the first node.
DSA Typescript
Hint
Start by defining the ListNode class with value and next. Then create three nodes and link them by setting the next property.
2
Create a traversal pointer
Create a variable called current and set it equal to head. This will help us move through the list.
DSA Typescript
Hint
Just create a variable current and assign it the value of head.
3
Write the iterative reverse function
Write a function called reverseList that takes a head of type ListNode | null and returns the reversed list head. Use three pointers: prev initialized to null, current initialized to head, and next to store the next node. Loop while current is not null, reverse the next pointer of current to prev, then move prev and current forward. Return prev as the new head.
DSA Typescript
Hint
Use three pointers: prev, current, and next. In each loop, reverse the link and move pointers forward.
4
Print the reversed linked list
Call reverseList with head and store the result in reversedHead. Then use a while loop with a variable node starting at reversedHead to print each node's value followed by ' -> '. After the loop, print 'null' to show the end of the list.
DSA Typescript
Hint
Call the reverseList function, then loop through the reversed list to build a string showing the values separated by ' -> ', ending with 'null'. Print the string.
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
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.
Step 2: Identify iterative approach
The iterative method changes links step-by-step using a loop without recursion.
Final Answer:
To change the direction of the links between nodes without using recursion -> Option B
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
Step 1: Understand pointer roles
Prev starts as null because the new tail points to null; current starts at head to traverse.
Step 2: Match initialization
Only let prev = null; let current = head; sets prev to null and current to head correctly for iteration.
Final Answer:
let prev = null; let current = head; -> Option A
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
Step 1: Trace the reversal loop
Initially prev=null, current=1. Each iteration reverses current.next to prev and moves pointers forward.
Step 2: Final reversed list
After loop, prev points to node 3, which links to 2, then 1, then null.
Final Answer:
3 -> 2 -> 1 -> null -> Option C
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
Step 1: Check pointer initialization
Prev must start as null because the new tail points to null, not head.
Step 2: Understand impact of wrong initialization
Starting prev at head causes a cycle or incorrect reversal.
Final Answer:
prev should be initialized to null, not head -> Option D
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?