Skip to content
CodeKruPro

Middle node of a linked list

Coding problemEasyAsked in: Adobe, Amazon, Flipkart, Microsoft, Samsung, Zoho

You are given the head of a non-empty singly linked list. Return its middle node. When the list has an even number of nodes there are two middle nodes; return the second one.

The result is shown as the values from the middle node to the end of the list.

Examples

Example 1
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Example 2
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Nodes 3 and 4 are both in the middle; return the second.
Example 3
Input: head = [9]
Output: [9]

Constraints

  • The list has between 1 and 100,000 nodes
  • Each value is a 32-bit integer

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

to answer and keep your progress. Reading is free for everyone.