DevPath

← All problems

Middle of the Linked List

Easy linked-list · two-pointers

Description

Return the value of the middle node. For an even length, return the second of the two middle nodes.

Constraints

1 <= n <= 100000

Examples

Input: list = [1,2,3,4,5]

Output: 3

Hints (1)
  • Slow and fast pointers; when fast reaches the end, slow is the middle.

Solutions

Slow and fast pointers. · Time O(n) · Space O(1)

func middleValue(list []int) int {
    return list[len(list)/2]
}

Create a free account to submit solutions, track progress and save problems.

Create account