Challenge - 5 Problems
Linked List Middle Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateFind the middle element output for odd length list
What is the output of the following C# code that finds the middle element of a linked list with 5 nodes?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public int FindMiddle() { Node slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } } LinkedList list = new LinkedList(); list.head = new Node(10); list.head.next = new Node(20); list.head.next.next = new Node(30); list.head.next.next.next = new Node(40); list.head.next.next.next.next = new Node(50); System.Console.WriteLine(list.FindMiddle());
Attempts:
2 left
💡 Hint
Use two pointers: one moves one step, the other moves two steps.
✗ Incorrect
The slow pointer moves one step at a time, fast pointer moves two steps. When fast reaches the end, slow is at the middle. For 5 nodes, middle is the 3rd node with value 30.
❓ Predict Output
intermediateFind the middle element output for even length list
What is the output of the following C# code that finds the middle element of a linked list with 6 nodes?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public int FindMiddle() { Node slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } } LinkedList list = new LinkedList(); list.head = new Node(5); list.head.next = new Node(10); list.head.next.next = new Node(15); list.head.next.next.next = new Node(20); list.head.next.next.next.next = new Node(25); list.head.next.next.next.next.next = new Node(30); System.Console.WriteLine(list.FindMiddle());
Attempts:
2 left
💡 Hint
For even length, the middle is the second of the two middle nodes.
✗ Incorrect
With 6 nodes, the slow pointer ends at the 4th node (value 20) when fast pointer reaches the end.
🔧 Debug
advancedIdentify the error in middle element code
What error does the following C# code produce when trying to find the middle element of a linked list?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public int FindMiddle() { Node slow = head, fast = head.next; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } } LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.head.next.next.next.next = new Node(5); System.Console.WriteLine(list.FindMiddle());
Attempts:
2 left
💡 Hint
Check initialization of fast pointer and loop condition.
✗ Incorrect
Starting fast at head.next causes fast.next.next to be called when fast is null, causing NullReferenceException.
🧠 Conceptual
advancedWhy use two pointers to find middle element?
Why is the two-pointer technique (slow and fast pointers) preferred to find the middle element of a linked list?
Attempts:
2 left
💡 Hint
Think about time and space efficiency.
✗ Incorrect
Two pointers move through the list once, slow pointer ends at middle. No extra memory needed.
❓ Predict Output
expertOutput of middle element after modifying linked list
What is the output of the following C# code after removing the middle element from the linked list?
DSA C#
public class Node { public int data; public Node next; public Node(int d) { data = d; next = null; } } public class LinkedList { public Node head; public int FindMiddle() { Node slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; } return slow.data; } public void RemoveMiddle() { if (head == null || head.next == null) { head = null; return; } Node slow = head, fast = head, prev = null; while (fast != null && fast.next != null) { prev = slow; slow = slow.next; fast = fast.next.next; } prev.next = slow.next; } } LinkedList list = new LinkedList(); list.head = new Node(1); list.head.next = new Node(2); list.head.next.next = new Node(3); list.head.next.next.next = new Node(4); list.head.next.next.next.next = new Node(5); list.RemoveMiddle(); System.Console.WriteLine(list.FindMiddle());
Attempts:
2 left
💡 Hint
After removing the middle, the list has 4 nodes. Find the new middle.
✗ Incorrect
Original middle 3 is removed. New list: 1->2->4->5. Middle is now 4 (the 3rd node).
