📖 Scenario: Imagine you have a chain of friends holding hands in a line. You want to find the friend standing exactly in the middle of the line.
🎯 Goal: You will create a simple linked list of numbers and write code to find and print the middle element in the list.
📋 What You'll Learn
Create a linked list with exactly 5 nodes containing the values 10, 20, 30, 40, and 50 in order.
Create a variable to help find the middle node using the two-pointer technique.
Write the logic to find the middle node by moving pointers through the list.
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 evenly or balancing tasks.
💼 Career
Understanding linked lists and pointer techniques is important for software developers working with data structures and algorithms.
Progress0 / 4 steps
1
Create the Linked List
Create a class called Node with an int data field and a Node next field. Then create 5 nodes with values 10, 20, 30, 40, and 50 linked in order, starting with a variable called head pointing to the first node.
DSA C#
Hint
Start by defining the Node class with data and next. Then create each node and link them by setting the next field.
2
Setup Two Pointers for Finding Middle
Create two variables called slow and fast and set both to point to head. These will help find the middle node by moving at different speeds.
DSA C#
Hint
Set both slow and fast to start at the head node.
3
Find the Middle Node Using Two Pointers
Use a while loop to move fast two steps and slow one step forward until fast reaches the end of the list. Use fast != null && fast.next != null as the loop condition.
DSA C#
Hint
Inside the loop, move slow one step and fast two steps forward until fast cannot move further.
4
Print the Middle Element
Write a Console.WriteLine statement to print the data value of the slow node, which is the middle element.
DSA C#
Hint
The middle node's data is stored in slow.data. Use Console.WriteLine(slow.data); to print it.
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. Reverse the list and pick the first element
B. Count all nodes first, then access the middle by index
C. Use two pointers moving at the same speed until they meet
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 movement
The slow pointer moves one node at a time, while the fast pointer moves two nodes at a time.
Step 2: Identify middle when fast pointer ends
When the fast pointer reaches the end (null), the 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 to find middle [OK]
Common Mistakes:
Moving both pointers at the same speed
Not checking for null before moving fast pointer
Counting nodes first instead of using pointers
2. Which of the following C# code snippets correctly initializes two pointers to find the middle of a linked list named head?
easy
A. ListNode slow = head; ListNode fast = head;
B. ListNode slow = null; ListNode fast = head;
C. ListNode slow = head.next; ListNode fast = head.next.next;
D. ListNode slow = head; ListNode fast = null;
Solution
Step 1: Check pointer initialization
Both slow and fast pointers should start at the head of the list to traverse correctly.
Step 2: Validate options
Only ListNode slow = head; ListNode fast = head; initializes both pointers at head, allowing proper traversal.
Final Answer:
ListNode slow = head; ListNode fast = head; -> Option A
Quick Check:
Both pointers start at head [OK]
Hint: Start both pointers at head to traverse linked list [OK]
Common Mistakes:
Starting slow or fast pointer at null
Starting fast pointer too far ahead causing null errors
Using head.next without checking for null
3. What will be the output of the following C# code that finds the middle element of a linked list?
class ListNode {
public int val;
public ListNode next;
public ListNode(int x) { val = x; next = null; }
}
ListNode head = new ListNode(10);
head.next = new ListNode(20);
head.next.next = new ListNode(30);
head.next.next.next = new ListNode(40);
head.next.next.next.next = new ListNode(50);
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Console.WriteLine(slow.val);
Slow pointer is at node with value 30, which is the middle of the list.
Final Answer:
30 -> Option A
Quick Check:
Middle element = 30 [OK]
Hint: Slow pointer lands on middle when fast reaches end [OK]
Common Mistakes:
Assuming middle is 20 or 40 without tracing
Not checking fast.next before moving fast
Confusing slow and fast pointer positions
4. The following code is intended to find the middle element of a linked list but throws a NullReferenceException. What is the error?
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
Console.WriteLine(slow.val);
medium
A. The fast pointer should start at head.next
B. The slow pointer should move two steps instead of one
C. The while condition should check if fast is null before fast.next
D. The loop should run while fast != null only
Solution
Step 1: Analyze while loop condition
The condition checks fast.next and fast.next.next but does not check if fast itself is null, which can cause NullReferenceException.
Step 2: Correct condition order
Check if fast != null before accessing fast.next to avoid exceptions.
Final Answer:
The while condition should check if fast is null before fast.next -> Option C
Quick Check:
Check fast != null before fast.next to avoid errors [OK]
Hint: Always check pointer is not null before accessing next [OK]
Common Mistakes:
Checking fast.next before fast itself
Moving slow pointer incorrectly
Starting fast pointer at wrong node
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 node instead of the second middle node?
hard
A. Move slow pointer two steps and fast pointer one step in the loop
B. Start fast pointer at head.next and slow at head, with the usual loop condition
C. Change the loop condition to 'while (fast.next != null && fast.next.next != null)' and move slow inside the loop
D. Use a stack to store nodes and pop half to get the first middle
Solution
Step 1: Understand default behavior
Normally, the two-pointer method returns the second middle node for even-length lists.
Step 2: Modify pointer start positions
Starting the fast pointer at head.next and slow at head with the usual loop condition causes the slow pointer to stop at the first middle node.
Final Answer:
Start fast pointer at head.next and slow at head, with the usual loop condition -> Option B
Quick Check:
Adjust pointer start positions to get first middle [OK]
Hint: Start fast one step ahead to get first middle [OK]
Common Mistakes:
Swapping slow and fast pointer speeds incorrectly
Starting fast pointer at wrong node
Using extra data structures unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.