Discover a clever trick that finds the middle of a list without counting every item!
Why Find Middle Element of Linked List in DSA Typescript?
Imagine you have a long chain of paper clips linked together. You want to find the paper clip right in the middle without counting each one from the start every time.
Counting each paper clip one by one is slow and tiring, especially if the chain is very long. You might lose track or make mistakes counting, and it wastes time.
Using a smart way, you can move two pointers along the chain: one moves one step at a time, the other moves two steps. When the faster one reaches the end, the slower one is exactly in the middle. This saves time and avoids mistakes.
let count = 0; let current = head; while (current !== null) { count++; current = current.next; } // Then traverse again to count/2
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
// slow is middleThis method lets you quickly find the middle of a linked list in just one pass, making your programs faster and simpler.
When playing a game with a queue of players, you might want to find the player in the middle to give a special role without counting everyone manually.
Manual counting is slow and error-prone.
Two-pointer technique finds middle in one pass.
Improves efficiency and reduces mistakes.
Practice
Solution
Step 1: Understand the two-pointer technique
One pointer moves one step at a time (slow), the other moves two steps at a time (fast).Step 2: Identify the middle when fast pointer reaches the end
When the fast pointer reaches the end of the list, the slow pointer will be at the middle node.Final Answer:
Use two pointers, one moving twice as fast as the other; when the fast pointer reaches the end, the slow pointer is at the middle. -> Option AQuick Check:
Two-pointer technique = slow at middle when fast ends [OK]
- Trying to count nodes first wastes extra time
- Reversing list is unnecessary and inefficient
- Using extra space like stack is not optimal
Solution
Step 1: Check the type of the next pointer
It should be either another Node or null if it is the last node.Step 2: Verify constructor initialization
The constructor sets value and initializes next to null correctly.Final Answer:
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option DQuick Check:
Next pointer type includes null and initialized to null [OK]
- Not allowing next to be null causes type errors
- Initializing next to undefined instead of null
- Using any type loses type safety
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
function findMiddle(head: Node | null): number | null {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow!.next;
fast = fast.next.next;
}
return slow ? slow.value : null;
}
// Create linked list: 10 -> 20 -> 30 -> 40 -> 50
const head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
console.log(findMiddle(head));Solution
Step 1: Trace slow and fast pointers through the list
Initially both at 10. After first loop: slow=20, fast=30; second loop: slow=30, fast=50; third loop ends as fast.next is null.Step 2: Identify slow pointer value at loop end
Slow pointer is at node with value 30, which is the middle.Final Answer:
30 -> Option BQuick Check:
Slow pointer stops at middle node value 30 [OK]
- Returning fast pointer value instead of slow
- Not checking fast.next in while condition
- Off-by-one errors in pointer movement
function findMiddle(head: Node | null): number | null {
let slow = head;
let fast = head;
while (fast !== null && fast.next !== null) {
slow = slow.next;
fast = fast.next.next;
}
return slow.value;
}Solution
Step 1: Analyze pointer null safety before accessing value
Slow pointer can be null if the list is empty, so accessing slow.value without check can cause runtime error.Step 2: Identify missing null check before return
The function should check if slow is not null before returning slow.value to avoid errors.Final Answer:
The function does not check if slow is null before accessing slow.value. -> Option CQuick Check:
Always check pointer is not null before accessing properties [OK]
- Ignoring null checks causes runtime errors
- Changing fast pointer movement breaks logic
- Returning fast.value misses middle element
Solution
Step 1: Understand difference between first and second middle in even-length list
For even nodes, two middle nodes exist; the second middle is the one after the first middle.Step 2: Modify fast pointer start to head.next
Starting fast pointer at head.next shifts slow pointer to first middle when fast reaches end.Final Answer:
Initialize fast pointer to head.next instead of head and then proceed normally. -> Option AQuick Check:
Fast starts at second node to get first middle [OK]
- Swapping slow and fast pointer steps breaks logic
- Using stack wastes extra space
- Counting nodes twice is inefficient
Ready to prove it?
Pass the test and this pattern moves unknown
