387. Middle of the Linked List

MediumLinked ListTwo PointersLinked List

Given a non-empty singly linked list, return the value of the middle node. If there are two middle nodes (even length), return the value of the second one.

Input: An array of node values.

Output: Integer — the middle node's value.

Examples

Example 1
Input: [1,2,3,4,5]
Output: 3
Explanation: Middle node is 3.
Example 2
Input: [1,2,3,4,5,6]
Output: 4
Explanation: Second middle for even length.
Example 3
Input: [1]
Output: 1
Explanation: Single node.

Constraints

Asked by

AccentureAmazonIBMGoogleMicrosoftMeta
Solve this problem in the editor →