📖 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 with 5 nodes, then finds and prints the middle element's value.
📋 What You'll Learn
Create a linked list with exactly 5 nodes with values 10, 20, 30, 40, 50
Use a pointer to traverse the list
Find the middle element using the slow and fast pointer technique
Print the middle element's value
💡 Why This Matters
🌍 Real World
Finding the middle element is useful in many applications like splitting data, balancing trees, or optimizing searches.
💼 Career
Understanding linked lists and pointer techniques is essential 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* called next. Then create 5 nodes named node1 to node5 with data values 10, 20, 30, 40, and 50 respectively. Link them so node1.next = &node2, node2.next = &node3, and so on, ending with node5.next = NULL. Finally, create a pointer head pointing to &node1.
DSA C
Hint
Remember to define the struct first, then create nodes with the given values, link them, and set the head pointer.
2
Create pointers for slow and fast traversal
Inside main, create two pointers called slow and fast and set both to point to head.
DSA C
Hint
Both slow and fast pointers start at the head of the list.
3
Find the middle element using slow and fast pointers
Use a while loop to move fast two steps and slow one step until fast is NULL or fast->next is NULL. This will make slow point to the middle node.
DSA C
Hint
Move slow by one node and fast by two nodes each loop until fast reaches the end.
4
Print the middle element's value
Use printf to print the data of the node pointed to by slow in the format: "Middle element: %d\n".
DSA C
Hint
Use printf("Middle element: %d\n", slow->data); to print the middle node's value.
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. Count all nodes first, then access the middle by index
B. Reverse the list and pick the first element
C. Use two pointers both moving one step at a time
D. Use one slow pointer moving one step and one fast pointer moving two steps until fast reaches the end
Solution
Step 1: Understand two-pointer technique
One pointer moves one node at a time (slow), the other moves two nodes at a time (fast).
Step 2: Identify middle when fast reaches end
When fast pointer reaches the end, slow pointer will be at the middle node.
Final Answer:
Use one slow pointer moving one step and one fast pointer moving two steps until fast reaches the end -> Option D
Quick Check:
Two-pointer technique = slow one step, fast two steps [OK]
Hint: Move fast pointer twice as fast as slow pointer [OK]
Common Mistakes:
Moving both pointers at the same speed
Counting nodes first instead of using pointers
Reversing the list unnecessarily
2. Which of the following C code snippets correctly initializes pointers to find the middle of a linked list named head?
easy
A. Node *slow = head; Node *fast = NULL;
B. Node *slow = head; Node *fast = head;
C. Node *slow = head->next; Node *fast = head->next;
D. Node *slow = NULL; Node *fast = head;
Solution
Step 1: Initialize both pointers at head
Both slow and fast pointers start at the head node to traverse the list properly.
Step 2: Check pointer initialization correctness
Only Node *slow = head; Node *fast = head; initializes both pointers correctly at head; others start one pointer at NULL or next node incorrectly.
Final Answer:
Node *slow = head; Node *fast = head; -> Option B
Quick Check:
Both pointers start at head [OK]
Hint: Start both pointers at head node [OK]
Common Mistakes:
Starting one pointer at NULL
Starting pointers at next node instead of head
Not initializing pointers before loop
3. What will be the output of the following C code snippet that finds the middle element of a linked list?
Change loop condition to while (fast != NULL && fast->next != NULL) -> Option A
Quick Check:
Check both fast and fast->next not NULL [OK]
Hint: Check both fast and fast->next in loop condition [OK]
Common Mistakes:
Not checking fast->next before accessing
Moving slow pointer incorrectly
Assuming code runs without null checks
5. Given a linked list with an even number of nodes, which node will the two-pointer method return as the middle element?
Consider the list: 5 -> 10 -> 15 -> 20 -> NULL
hard
A. 15 (the second middle node)
B. 10 (the first middle node)
C. 5 (the head node)
D. 20 (the last node)
Solution
Step 1: Trace pointers on even-length list
Start slow and fast at 5. After first iteration, slow=10, fast=15. Next iteration, slow=15, fast=NULL (end).
Step 2: Identify middle node returned
When fast reaches NULL, slow points to 15, which is the second middle node in even-length list.
Final Answer:
15 (the second middle node) -> Option A
Quick Check:
Two-pointer method returns second middle in even list [OK]
Hint: Two-pointer method returns second middle in even-length list [OK]
Common Mistakes:
Assuming first middle is returned
Confusing head or last node as middle
Not tracing pointer steps carefully
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.