📖 Scenario: You are working with a simple linked list that stores numbers. You want to find the middle number in the list, just like finding the middle book on a shelf.
🎯 Goal: Build a program that creates a linked list, sets up a pointer to traverse it, finds the middle element, and prints its value.
📋 What You'll Learn
Create a linked list with exactly 5 nodes containing values 10, 20, 30, 40, 50
Create a pointer variable called slow and another called fast to traverse the list
Use a loop to move slow by one node and fast by two nodes until fast reaches the end
Print the value of the middle node pointed by slow
💡 Why This Matters
🌍 Real World
Finding the middle element is useful in many applications like splitting data, balancing tasks, or optimizing searches.
💼 Career
Understanding linked list traversal and pointer manipulation is fundamental for software developers working with low-level data structures and memory management.
Progress0 / 4 steps
1
Create the linked list with 5 nodes
Create a struct called Node with an int data and a Node* next. Then create 5 nodes with values 10, 20, 30, 40, and 50 linked together starting from a pointer called head.
DSA C++
Hint
Remember to link each node's next pointer to the next node.
2
Create pointers to traverse the list
Create two pointer variables called slow and fast and set both to point to head.
DSA C++
Hint
Both pointers start at the head of the list.
3
Move pointers to find the middle node
Use a while loop with the condition fast != nullptr && fast->next != nullptr. Inside the loop, move slow one step forward (slow = slow->next) and fast two steps forward (fast = fast->next->next).
DSA C++
Hint
Move slow by one and fast by two nodes until fast reaches the end.
4
Print the middle element
Print the value of the middle node using cout and slow->data.
DSA C++
Hint
The middle element of the list is the value stored in slow->data.
Practice
(1/5)
1. What is the main idea behind finding the middle element of a singly linked list using the two-pointer approach?
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 the total nodes first, then traverse again to the middle node.
C. Reverse the linked list and pick the first element.
D. Use a stack to store nodes and pop half of them.
Solution
Step 1: Understand the two-pointer technique
The fast pointer moves two nodes at a time, and the slow pointer moves one node at a time.
Step 2: Identify the middle when fast pointer reaches the end
When the fast pointer reaches the end (null), 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 approach = Fast and slow pointers [OK]
Hint: Move one pointer twice as fast; slow pointer lands in middle [OK]
Common Mistakes:
Trying to reverse the list unnecessarily
Counting nodes twice instead of one pass
Using extra space like stack without need
2. Which of the following C++ code snippets correctly initializes the slow and fast pointers to find the middle of a linked list named head?
easy
A. ListNode* slow = head->next; ListNode* fast = head;
B. ListNode* slow = nullptr; ListNode* fast = head;
C. ListNode* slow = head; ListNode* fast = nullptr;
D. ListNode* slow = head; ListNode* fast = head;
Solution
Step 1: Initialize both pointers at the head
Both slow and fast pointers should start at the head to traverse the list correctly.
Step 2: Avoid null initialization for slow or fast
Initializing slow or fast to nullptr will cause errors or skip nodes.
Final Answer:
ListNode* slow = head; ListNode* fast = head; -> Option D
Quick Check:
Both pointers start at head [OK]
Hint: Start both pointers at head node [OK]
Common Mistakes:
Initializing pointers to nullptr causing runtime errors
Starting slow pointer at head->next skipping first node
Starting fast pointer at nullptr causing immediate stop
3. What will be the output of the following C++ code snippet that finds the middle element of a linked list?
Step 1: Trace slow and fast pointers through the list
Initially, slow=1, fast=1; after first loop, slow=2, fast=3; second loop, slow=3, fast=5; next loop fast->next is nullptr, stop.
Step 2: Identify slow pointer position at loop end
Slow pointer is at node with data 3, which is the middle element.
Final Answer:
3 -> Option A
Quick Check:
Middle element = 3 [OK]
Hint: Fast moves two steps, slow moves one; slow ends at middle [OK]
Common Mistakes:
Assuming middle is 2 or 4 instead of 3
Not checking fast->next before moving fast
Confusing zero-based indexing with node values
4. Identify the error in the following code snippet that attempts to find the middle element of a linked list and fix it:
Node* findMiddle(Node* head) {
Node* slow = head;
Node* fast = head;
while (fast != nullptr) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
medium
A. Return fast pointer instead of slow.
B. The while condition should check both fast and fast->next to avoid null pointer dereference.
C. Move slow pointer two steps instead of one.
D. Initialize slow to nullptr instead of head.
Solution
Step 1: Analyze the while loop condition
The loop only checks if fast != nullptr, but fast->next might be nullptr causing a crash when accessing fast->next->next.
Step 2: Correct the condition to check fast->next as well
Change the loop condition to while (fast != nullptr && fast->next != nullptr) to safely move fast pointer two steps.
Final Answer:
The while condition should check both fast and fast->next to avoid null pointer dereference. -> Option B
Quick Check:
Check fast and fast->next before moving fast [OK]
Hint: Always check fast and fast->next in loop condition [OK]
Common Mistakes:
Not checking fast->next causing runtime errors
Moving slow pointer two steps incorrectly
Returning wrong pointer at the end
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 slow pointer to head->next instead of head.
B. Move fast pointer one step at a time instead of two.
C. Initialize fast pointer to head->next instead of head.
D. Use a stack to store nodes and pop half.
Solution
Step 1: Understand default behavior for even length lists
Normally, with both pointers starting at head, slow ends at the second middle node for even length lists.
Step 2: Modify fast pointer start to head->next
Starting fast at head->next causes slow to stop at the first middle node when fast reaches the end.
Final Answer:
Initialize fast pointer to head->next instead of head. -> Option C
Quick Check:
Fast starts at head->next for first middle [OK]
Hint: Start fast at second node to get first middle in even list [OK]
Common Mistakes:
Changing slow pointer start instead of fast
Moving fast pointer one step breaks logic
Using extra space unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.