BackeasyLinked List

Middle Node Value Retrieval Solution

Problem Statement

Given a singly linked list, find the value stored in the middle node. If the linked list contains an even number of nodes, return the value of the second middle node. You must implement this by traversing the list only once using the fast and slow pointer technique.

Example 1
Input
1 -> 2 -> 3 -> 4 -> 5
Output
3

Explanation: The list has 5 nodes, so the middle node is 3.

Example 2
Input
10 -> 20 -> 30 -> 40
Output
30

Explanation: The list has 4 nodes. The middle nodes are 20 and 30; the second middle node is 30.

Constraints

  • The number of nodes in the list is between 1 and 1000.
  • Node values are integers between -10^5 and 10^5.
  • The solution must operate in O(n) time complexity.
  • The solution must use O(1) extra space.
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free