Asked at

Middle Of The Linked List

Easy
Verified
Linked ListTwo Pointers~12 min

Given the values of a singly linked list as an array head (head first), return the list starting from its middle node.

For an even number of nodes, return from the second middle node. Use the slow/fast two-pointer trick to find it in one pass.

The input arrives as a single array head (number[]).

Examples

in[1, 2, 3, 4, 5]
out[3, 4, 5]

The middle node holds 3; return it and everything after.

in[1, 2, 3, 4, 5, 6]
out[4, 5, 6]

Even length, so return the second of the two middle nodes.

Constraints

  • The number of nodes is in the range [1, 100].
  • 1 ≤ Node.val ≤ 100
  • Target: O(n) time, single pass.

Get help

🔑

Sign in to solve

Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.