Bird
Raised Fist0
DSA Cprogramming~20 mins

Find Middle Element of Linked List in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Middle Element Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Find 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
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

int printMiddle(struct Node* head) {
    struct Node *slow_ptr = head;
    struct Node *fast_ptr = head;

    if (head != NULL) {
        while (fast_ptr != NULL && fast_ptr->next != NULL) {
            fast_ptr = fast_ptr->next->next;
            slow_ptr = slow_ptr->next;
        }
        return slow_ptr->data;
    }
    return -1;
}

int main() {
    struct Node* head = NULL;
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    int middle = printMiddle(head);
    printf("%d\n", middle);
    return 0;
}
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Remember the slow pointer moves one step while the fast pointer moves two steps.
❓ Predict Output
intermediate
2:00remaining
Find 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
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

int printMiddle(struct Node* head) {
    struct Node *slow_ptr = head;
    struct Node *fast_ptr = head;

    if (head != NULL) {
        while (fast_ptr != NULL && fast_ptr->next != NULL) {
            fast_ptr = fast_ptr->next->next;
            slow_ptr = slow_ptr->next;
        }
        return slow_ptr->data;
    }
    return -1;
}

int main() {
    struct Node* head = NULL;
    push(&head, 6);
    push(&head, 5);
    push(&head, 4);
    push(&head, 3);
    push(&head, 2);
    push(&head, 1);
    int middle = printMiddle(head);
    printf("%d\n", middle);
    return 0;
}
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
For even length lists, the slow pointer stops at the second middle node.
🔧 Debug
advanced
2:00remaining
Identify the error in middle element function
What error will the following C code produce when trying to find the middle element of a linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

int printMiddle(struct Node* head) {
    struct Node *slow_ptr = head;
    struct Node *fast_ptr = head;

    while (fast_ptr->next != NULL && fast_ptr->next->next != NULL) {
        slow_ptr = slow_ptr->next;
        fast_ptr = fast_ptr->next->next;
    }
    return slow_ptr->data;
}

int main() {
    struct Node* head = NULL;
    // Empty list
    int middle = printMiddle(head);
    printf("%d\n", middle);
    return 0;
}
ASegmentation fault (accessing NULL pointer)
BReturns -1
CReturns 0
DCompilation error
Attempts:
2 left
💡 Hint
Check what happens when head is NULL and you access fast_ptr->next.
🧠 Conceptual
advanced
1:30remaining
Why use two pointers to find middle element?
Why does the two-pointer technique (slow and fast pointers) efficiently find the middle element of a linked list?
ABecause both pointers move at the same speed to find the middle
BBecause the slow pointer counts the nodes while the fast pointer stores the middle value
CBecause the fast pointer moves backward to find the middle
DBecause the fast pointer moves twice as fast, when it reaches the end, the slow pointer is at the middle
Attempts:
2 left
💡 Hint
Think about relative speeds of pointers.
❓ Predict Output
expert
2:30remaining
Output of middle element after list modification
What is the output of the following C code after modifying the linked list and finding the middle element?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data) {
    struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
    new_node->data = new_data;
    new_node->next = (*head_ref);
    (*head_ref) = new_node;
}

int printMiddle(struct Node* head) {
    struct Node *slow_ptr = head;
    struct Node *fast_ptr = head;

    if (head != NULL) {
        while (fast_ptr != NULL && fast_ptr->next != NULL) {
            fast_ptr = fast_ptr->next->next;
            slow_ptr = slow_ptr->next;
        }
        return slow_ptr->data;
    }
    return -1;
}

int main() {
    struct Node* head = NULL;
    push(&head, 10);
    push(&head, 20);
    push(&head, 30);
    push(&head, 40);
    push(&head, 50);
    // List is 50->40->30->20->10
    // Remove head node
    struct Node* temp = head;
    head = head->next;
    free(temp);
    // Now list is 40->30->20->10
    int middle = printMiddle(head);
    printf("%d\n", middle);
    return 0;
}
A20
B40
C30
D10
Attempts:
2 left
💡 Hint
After removing the first node, find the middle of the new list with 4 nodes.

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