Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Middle Node

Linked Listseasy

A linked list is given as an array of values. Implement `middleNode(arr)` returning the middle value using the slow and fast pointer technique. For even length, return the second of the two middle values.

Sample tests

Input: middleNode([1,2,3,4,5])
Output: 3
Input: middleNode([1,2,3,4])
Output: 3

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • The loop condition determines which middle you get for even length.

Learning resources

  • Wikipedia: Data structure
Approach & explanation (try first)

Moving fast by two and slow by one lands slow at the middle in a single pass.

Loading...
⌘/Ctrl + Enter

Run your code to see results.