Bird
Raised Fist0
DSA Cprogramming

Find Middle Element of Linked List in DSA C

Choose your learning style10 modes available
Mental Model
To find the middle element, use two pointers moving at different speeds so when the fast one reaches the end, the slow one is in the middle.
Analogy: Imagine two friends walking on a path: one walks twice as fast as the other. When the faster friend reaches the end, the slower friend is exactly halfway.
head -> 1 -> 2 -> 3 -> 4 -> 5 -> null
slow ↑
fast ↑
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3 -> 4 -> 5 -> null
Goal: Find the middle element of the linked list
Step 1: Initialize slow and fast pointers at head (node 1)
head -> [slow->1, fast->1] -> 2 -> 3 -> 4 -> 5 -> null
Why: Both pointers start at the beginning to start traversal
Step 2: Move slow pointer one step to node 2, fast pointer two steps to node 3
head -> 1 -> [slow->2] -> [fast->3] -> 4 -> 5 -> null
Why: Slow moves one step, fast moves two steps to find middle efficiently
Step 3: Move slow pointer one step to node 3, fast pointer two steps to node 5
head -> 1 -> 2 -> [slow->3] -> 4 -> [fast->5] -> null
Why: Continue moving pointers until fast reaches end
Step 4: Try to move fast pointer two steps but reaches null, stop
head -> 1 -> 2 -> [slow->3] -> 4 -> 5 -> null
Why: Fast pointer reached end, slow pointer is at middle
Result:
head -> 1 -> 2 -> [3] -> 4 -> 5 -> null
Middle element is 3
Annotated Code
DSA C
#include <stdio.h>
#include <stdlib.h>

// Node structure
typedef struct Node {
    int data;
    struct Node* next;
} Node;

// Function to create a new node
Node* createNode(int data) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}

// Function to find middle element
int findMiddle(Node* head) {
    if (head == NULL) return -1; // empty list

    Node* slow = head;
    Node* fast = head;

    while (fast != NULL && fast->next != NULL) {
        slow = slow->next; // move slow one step
        fast = fast->next->next; // move fast two steps
    }

    return slow->data; // slow is at middle
}

// Function to print linked list
void printList(Node* head) {
    Node* curr = head;
    while (curr != NULL) {
        printf("%d -> ", curr->data);
        curr = curr->next;
    }
    printf("null\n");
}

int main() {
    // Create linked list 1->2->3->4->5->null
    Node* head = createNode(1);
    head->next = createNode(2);
    head->next->next = createNode(3);
    head->next->next->next = createNode(4);
    head->next->next->next->next = createNode(5);

    printList(head);
    int middle = findMiddle(head);
    printf("Middle element is %d\n", middle);

    return 0;
}
while (fast != NULL && fast->next != NULL) {
continue loop while fast pointer and next node exist
slow = slow->next; // move slow one step
advance slow pointer by one node to approach middle
fast = fast->next->next; // move fast two steps
advance fast pointer by two nodes to reach end faster
return slow->data; // slow is at middle
slow pointer now points to middle node, return its data
OutputSuccess
1 -> 2 -> 3 -> 4 -> 5 -> null Middle element is 3
Complexity Analysis
Time: O(n) because we traverse the list once with two pointers
Space: O(1) because only two pointers are used regardless of list size
vs Alternative: Better than counting nodes first then traversing again, which takes two passes (O(2n))
Edge Cases
Empty list
Returns -1 indicating no middle element
DSA C
if (head == NULL) return -1; // empty list
List with one element
Returns the single element as middle
DSA C
while (fast != NULL && fast->next != NULL)
List with even number of elements
Returns the second middle element (e.g., for 4 elements returns 3rd node)
DSA C
while (fast != NULL && fast->next != NULL)
When to Use This Pattern
When you need to find the middle of a linked list efficiently, use two pointers moving at different speeds to find it in one pass.
Common Mistakes
Mistake: Moving both pointers one step at a time and then counting steps to find middle
Fix: Move one pointer twice as fast as the other to find middle in one traversal
Mistake: Not checking if fast or fast->next is NULL before moving fast two steps
Fix: Add condition while (fast != NULL && fast->next != NULL) to avoid null pointer errors
Summary
Finds the middle element of a linked list using two pointers moving at different speeds.
Use when you want to find the middle node in a single pass without counting nodes first.
The slow pointer will be at the middle when the fast pointer reaches the end.

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

  1. Step 1: Understand two-pointer technique

    One pointer moves one node at a time (slow), the other moves two nodes at a time (fast).
  2. Step 2: Identify middle when fast reaches end

    When fast pointer reaches the end, slow pointer will be at the middle node.
  3. Final Answer:

    Use one slow pointer moving one step and one fast pointer moving two steps until fast reaches the end -> Option D
  4. 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

  1. Step 1: Initialize both pointers at head

    Both slow and fast pointers start at the head node to traverse the list properly.
  2. 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.
  3. Final Answer:

    Node *slow = head; Node *fast = head; -> Option B
  4. 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?
typedef struct Node {
  int data;
  struct Node *next;
} Node;

int main() {
  Node n3 = {30, NULL};
  Node n2 = {20, &n3};
  Node n1 = {10, &n2};
  Node *head = &n1;

  Node *slow = head, *fast = head;
  while (fast != NULL && fast->next != NULL) {
    slow = slow->next;
    fast = fast->next->next;
  }
  printf("%d", slow->data);
  return 0;
}
medium
A. 30
B. 10
C. 20
D. Runtime error

Solution

  1. Step 1: Trace pointer movements

    Initially, slow and fast point to 10. After first loop, slow moves to 20, fast moves to 30. Next loop fast->next is NULL, loop ends.
  2. Step 2: Identify slow pointer position

    Slow pointer is at node with data 20, which is the middle element.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    Middle element = 20 [OK]
Hint: Slow pointer stops at middle when fast reaches end [OK]
Common Mistakes:
  • Assuming middle is first node
  • Confusing fast pointer position with slow
  • Ignoring loop exit condition
4. Identify the error in the following code snippet intended to find the middle element of a linked list and select the correct fix.
Node *slow = head;
Node *fast = head;
while (fast != NULL) {
  slow = slow->next;
  fast = fast->next->next;
}
return slow->data;
medium
A. Change loop condition to while (fast != NULL && fast->next != NULL)
B. Initialize slow to head->next instead of head
C. Move slow pointer two steps instead of one
D. No error, code is correct

Solution

  1. Step 1: Analyze loop condition

    The loop checks only fast != NULL, but fast->next could be NULL causing fast->next->next to crash.
  2. Step 2: Correct loop condition

    Adding fast->next != NULL prevents dereferencing NULL pointer, avoiding runtime error.
  3. Final Answer:

    Change loop condition to while (fast != NULL && fast->next != NULL) -> Option A
  4. 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

  1. 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).
  2. Step 2: Identify middle node returned

    When fast reaches NULL, slow points to 15, which is the second middle node in even-length list.
  3. Final Answer:

    15 (the second middle node) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard