05 / 16

How do you find the middle of a Linked List in one pass?

Finding the Middle Node

javascript
  1. 1

    Time complexity: O(n).

  2. 2

    Auxiliary space: O(1).

  3. 3

    For an odd-length list, slow identifies the unique middle.

  4. 4

    For an even-length list, this initialization returns the second middle node.

  5. 5

    The choice of first or second middle can be changed by adjusting the pointer initialization or loop condition.

Difficulty: 2/10

Follow-up Questions

  • How do you return the first middle for an even-length list?
  • Can this technique be used for cycle detection?