📖 Scenario: Imagine you have a line of people waiting for a bus. You want to find the person standing exactly in the middle of the line.In programming, we can represent this line as a linked list, where each person is a node pointing to the next person.
🎯 Goal: You will create a linked list, then write code to find and print the middle element's value.
📋 What You'll Learn
Create a linked list with exactly 5 nodes with values 10, 20, 30, 40, 50
Create a variable to help find the middle element
Write the logic to find the middle element using the two-pointer technique
Print the value of the middle node
💡 Why This Matters
🌍 Real World
Finding the middle element is useful in many real-world problems like splitting data, balancing tasks, or optimizing searches.
💼 Career
Understanding linked lists and pointer techniques is important for software engineering roles, especially in system design and algorithm optimization.
Progress0 / 4 steps
1
Create a Linked List with 5 Nodes
Create a class called ListNode with a constructor that takes a value of type number and a next pointer initialized to null. Then create 5 nodes with values 10, 20, 30, 40, and 50 linked together in a variable called head.
DSA Typescript
Hint
Start by defining the ListNode class with value and next. Then link nodes one by one.
2
Create Two Pointer Variables
Create two variables called slow and fast and set both to head. These will help us find the middle element.
DSA Typescript
Hint
Both slow and fast start at the head of the list.
3
Find the Middle Element Using Two Pointers
Use a while loop with the condition fast !== null && fast.next !== null. Inside the loop, move slow one step forward (slow = slow.next) and fast two steps forward (fast = fast.next.next).
DSA Typescript
Hint
Move slow by one and fast by two steps until fast reaches the end.
4
Print the Middle Element's Value
Write console.log(slow!.value) to print the value of the middle node.
DSA Typescript
Hint
The middle node value is printed after the loop ends.
Practice
(1/5)
1. What is the main idea behind finding the middle element of a singly linked list using the two-pointer technique?
easy
A. 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.
B. Count all nodes first, then traverse again to the middle index.
C. Reverse the linked list and pick the middle node from the reversed list.
D. Use a stack to store all nodes and pop half of them to find the middle.
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 A
Quick Check:
Two-pointer technique = slow at middle when fast ends [OK]
Hint: Move one pointer twice as fast to find middle easily [OK]
Common Mistakes:
Trying to count nodes first wastes extra time
Reversing list is unnecessary and inefficient
Using extra space like stack is not optimal
2. Which of the following TypeScript code snippets correctly initializes a singly linked list node?
easy
A. class Node { value: number; next: any; constructor(value: number) { this.value = value; this.next = undefined; } }
B. class Node { value: number; next: Node; constructor(value: number) { this.value = value; this.next = null; } }
C. class Node { value: number; next: null; constructor(value: number) { this.value = value; this.next = null; } }
Next pointer type includes null and initialized to null [OK]
Hint: Next pointer must allow null for list end [OK]
Common Mistakes:
Not allowing next to be null causes type errors
Initializing next to undefined instead of null
Using any type loses type safety
3. What will be the output of the following TypeScript code that finds the middle element of a linked list?
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));
medium
A. 20
B. 30
C. 40
D. 50
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 B
Quick Check:
Slow pointer stops at middle node value 30 [OK]
Hint: Fast pointer moves twice, slow pointer lands on middle [OK]
Common Mistakes:
Returning fast pointer value instead of slow
Not checking fast.next in while condition
Off-by-one errors in pointer movement
4. Identify the error in the following TypeScript function that attempts to find the middle element of a linked list:
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;
}
medium
A. The fast pointer should move one step at a time, not two.
B. The while loop condition should be while (fast !== null) only.
C. The function does not check if slow is null before accessing slow.value.
D. The function should return fast.value instead of 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 C
Quick Check:
Always check pointer is not null before accessing properties [OK]
Hint: Check for null before accessing node properties [OK]
Common Mistakes:
Ignoring null checks causes runtime errors
Changing fast pointer movement breaks logic
Returning fast.value misses middle element
5. Given a singly linked list with an even number of nodes, how can you modify the two-pointer approach to return the first middle element instead of the second middle element?
hard
A. Initialize fast pointer to head.next instead of head and then proceed normally.
B. Move slow pointer two steps and fast pointer one step in each iteration.
C. Use a stack to store nodes and pop half to get the second middle.
D. Traverse the list twice: first to count nodes, second to middle+1 node.
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 A
Quick Check:
Fast starts at second node to get first middle [OK]
Hint: Start fast pointer one step ahead for first middle [OK]
Common Mistakes:
Swapping slow and fast pointer steps breaks logic
Using stack wastes extra space
Counting nodes twice is inefficient
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.