Search⌘ K
AI Features

Middle of the Linked List

Explore how to find the middle node in a singly linked list by employing the fast and slow pointer technique. This lesson helps you understand the concept, implement a solution, and apply this pattern to handle even or odd numbers of nodes efficiently.

Statement

Given the head of a singly linked list, return the middle node of the linked list. If the number of nodes in the linked list is even, there will be two middle nodes, so return the second one.

Constraints:

Let n be the number of nodes in a linked list.

  • 11 \leq n 100\leq 100
  • 11 \leq Node.value 100\leq 100
  • head \neq NULL

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Middle of the Linked List

1.

What is the output if the following linked list is given as input?

2 → 4 → 6 → 8 → 10 → NULL

A.

4

B.

6

C.

2

D.

8


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > Solution.cpp
// Definition for a Linked List node
// class ListNode {
// public:
// int val;
// ListNode* next;
// // Constructor
// ListNode(int val = 0, ListNode* next = nullptr);
// };
ListNode* GetMiddleNode(ListNode* head){
// Replace this placeholder return statement with your code
return head;
}
Middle of the Linked List