Bird
Raised Fist0
DSA C++programming~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 of a linked list
What is the output of the following C++ code that finds the middle element of a singly linked list?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* findMiddle(Node* head) {
    Node* slow = head;
    Node* fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

int main() {
    Node* head = new Node(10);
    head->next = new Node(20);
    head->next->next = new Node(30);
    head->next->next->next = new Node(40);
    head->next->next->next->next = new Node(50);

    Node* mid = findMiddle(head);
    if (mid) {
        std::cout << mid->data << std::endl;
    }
    return 0;
}
A30
B20
C40
D50
Attempts:
2 left
💡 Hint
Use two pointers: one moves one step, the other moves two steps.
❓ Predict Output
intermediate
2:00remaining
Output when linked list has even number of nodes
What will be printed by this code when the linked list has 6 nodes?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* findMiddle(Node* head) {
    Node* slow = head;
    Node* fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);
    head->next->next->next = new Node(4);
    head->next->next->next->next = new Node(5);
    head->next->next->next->next->next = new Node(6);

    Node* mid = findMiddle(head);
    if (mid) {
        std::cout << mid->data << std::endl;
    }
    return 0;
}
A3
B4
C2
D5
Attempts:
2 left
💡 Hint
For even nodes, the function returns the second middle node.
🔧 Debug
advanced
2:00remaining
Identify the error in this middle element finder
What error does this code produce when trying to find the middle element of a linked list?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* findMiddle(Node* head) {
    Node* slow = head;
    Node* fast = head->next;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

int main() {
    Node* head = new Node(1);
    head->next = new Node(2);
    head->next->next = new Node(3);

    Node* mid = findMiddle(head);
    if (mid) {
        std::cout << mid->data << std::endl;
    }
    return 0;
}
AOutputs 3
BOutputs 1
COutputs 2
DSegmentation fault (runtime error)
Attempts:
2 left
💡 Hint
Check where fast pointer starts and how it moves.
❓ Predict Output
advanced
2:00remaining
Output of middle element after deleting nodes
Given the linked list and operations below, what is the middle element printed at the end?
DSA C++
struct Node {
    int data;
    Node* next;
    Node(int val) : data(val), next(nullptr) {}
};

Node* findMiddle(Node* head) {
    Node* slow = head;
    Node* fast = head;
    while (fast && fast->next) {
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

void deleteNode(Node*& head, int val) {
    if (!head) return;
    if (head->data == val) {
        Node* temp = head;
        head = head->next;
        delete temp;
        return;
    }
    Node* curr = head;
    while (curr->next && curr->next->data != val) {
        curr = curr->next;
    }
    if (curr->next) {
        Node* temp = curr->next;
        curr->next = curr->next->next;
        delete temp;
    }
}

int main() {
    Node* head = new Node(5);
    head->next = new Node(10);
    head->next->next = new Node(15);
    head->next->next->next = new Node(20);
    head->next->next->next->next = new Node(25);

    deleteNode(head, 15);
    deleteNode(head, 5);

    Node* mid = findMiddle(head);
    if (mid) {
        std::cout << mid->data << std::endl;
    }
    return 0;
}
A20
B25
C10
D15
Attempts:
2 left
💡 Hint
After deletions, list is 10 -> 20 -> 25. Find middle of this list.
🧠 Conceptual
expert
2:00remaining
Why does the two-pointer method find the middle node?
Why does moving one pointer twice as fast as the other find the middle node of a linked list?
ABecause the slow pointer moves twice as fast as the fast pointer.
BBecause the slow pointer skips every other node, landing on the middle.
CBecause the fast pointer counts the nodes and tells the slow pointer where to stop.
DBecause the fast pointer reaches the end in half the steps, so the slow pointer is at the middle.
Attempts:
2 left
💡 Hint
Think about relative speeds and positions of pointers.

Practice

(1/5)
1. What is the main idea behind finding the middle element of a singly linked list using the two-pointer approach?
easy
A. Use two pointers, one moving twice as fast as the other; when the fast pointer reaches the end, the slow pointer is at the middle.
B. Count the total nodes first, then traverse again to the middle node.
C. Reverse the linked list and pick the first element.
D. Use a stack to store nodes and pop half of them.

Solution

  1. Step 1: Understand the two-pointer technique

    The fast pointer moves two nodes at a time, and the slow pointer moves one node at a time.
  2. Step 2: Identify the middle when fast pointer reaches the end

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

    Use two pointers, one moving twice as fast as the other; when the fast pointer reaches the end, the slow pointer is at the middle. -> Option A
  4. Quick Check:

    Two-pointer approach = Fast and slow pointers [OK]
Hint: Move one pointer twice as fast; slow pointer lands in middle [OK]
Common Mistakes:
  • Trying to reverse the list unnecessarily
  • Counting nodes twice instead of one pass
  • Using extra space like stack without need
2. Which of the following C++ code snippets correctly initializes the slow and fast pointers to find the middle of a linked list named head?
easy
A. ListNode* slow = head->next; ListNode* fast = head;
B. ListNode* slow = nullptr; ListNode* fast = head;
C. ListNode* slow = head; ListNode* fast = nullptr;
D. ListNode* slow = head; ListNode* fast = head;

Solution

  1. Step 1: Initialize both pointers at the head

    Both slow and fast pointers should start at the head to traverse the list correctly.
  2. Step 2: Avoid null initialization for slow or fast

    Initializing slow or fast to nullptr will cause errors or skip nodes.
  3. Final Answer:

    ListNode* slow = head; ListNode* fast = head; -> Option D
  4. Quick Check:

    Both pointers start at head [OK]
Hint: Start both pointers at head node [OK]
Common Mistakes:
  • Initializing pointers to nullptr causing runtime errors
  • Starting slow pointer at head->next skipping first node
  • Starting fast pointer at nullptr causing immediate stop
3. What will be the output of the following C++ code snippet that finds the middle element of a linked list?
struct Node {
  int data;
  Node* next;
};

Node* findMiddle(Node* head) {
  Node* slow = head;
  Node* fast = head;
  while (fast != nullptr && fast->next != nullptr) {
    slow = slow->next;
    fast = fast->next->next;
  }
  return slow;
}

int main() {
  Node n1 = {1, nullptr};
  Node n2 = {2, nullptr};
  Node n3 = {3, nullptr};
  Node n4 = {4, nullptr};
  Node n5 = {5, nullptr};
  n1.next = &n2;
  n2.next = &n3;
  n3.next = &n4;
  n4.next = &n5;
  Node* mid = findMiddle(&n1);
  std::cout << mid->data << std::endl;
  return 0;
}
medium
A. 3
B. 2
C. 4
D. 5

Solution

  1. Step 1: Trace slow and fast pointers through the list

    Initially, slow=1, fast=1; after first loop, slow=2, fast=3; second loop, slow=3, fast=5; next loop fast->next is nullptr, stop.
  2. Step 2: Identify slow pointer position at loop end

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

    3 -> Option A
  4. Quick Check:

    Middle element = 3 [OK]
Hint: Fast moves two steps, slow moves one; slow ends at middle [OK]
Common Mistakes:
  • Assuming middle is 2 or 4 instead of 3
  • Not checking fast->next before moving fast
  • Confusing zero-based indexing with node values
4. Identify the error in the following code snippet that attempts to find the middle element of a linked list and fix it:
Node* findMiddle(Node* head) {
  Node* slow = head;
  Node* fast = head;
  while (fast != nullptr) {
    slow = slow->next;
    fast = fast->next->next;
  }
  return slow;
}
medium
A. Return fast pointer instead of slow.
B. The while condition should check both fast and fast->next to avoid null pointer dereference.
C. Move slow pointer two steps instead of one.
D. Initialize slow to nullptr instead of head.

Solution

  1. Step 1: Analyze the while loop condition

    The loop only checks if fast != nullptr, but fast->next might be nullptr causing a crash when accessing fast->next->next.
  2. Step 2: Correct the condition to check fast->next as well

    Change the loop condition to while (fast != nullptr && fast->next != nullptr) to safely move fast pointer two steps.
  3. Final Answer:

    The while condition should check both fast and fast->next to avoid null pointer dereference. -> Option B
  4. Quick Check:

    Check fast and fast->next before moving fast [OK]
Hint: Always check fast and fast->next in loop condition [OK]
Common Mistakes:
  • Not checking fast->next causing runtime errors
  • Moving slow pointer two steps incorrectly
  • Returning wrong pointer at the end
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 element instead of the second middle element?
hard
A. Initialize slow pointer to head->next instead of head.
B. Move fast pointer one step at a time instead of two.
C. Initialize fast pointer to head->next instead of head.
D. Use a stack to store nodes and pop half.

Solution

  1. Step 1: Understand default behavior for even length lists

    Normally, with both pointers starting at head, slow ends at the second middle node for even length lists.
  2. Step 2: Modify fast pointer start to head->next

    Starting fast at head->next causes slow to stop at the first middle node when fast reaches the end.
  3. Final Answer:

    Initialize fast pointer to head->next instead of head. -> Option C
  4. Quick Check:

    Fast starts at head->next for first middle [OK]
Hint: Start fast at second node to get first middle in even list [OK]
Common Mistakes:
  • Changing slow pointer start instead of fast
  • Moving fast pointer one step breaks logic
  • Using extra space unnecessarily
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