Middle of the Linked List
Easy linked-list · two-pointers
Description
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]
}
Slow and fast pointers. · Time O(n) · Space O(1)
function middleValue(list) {
return list[Math.floor(list.length / 2)];
}
Slow and fast pointers. · Time O(n) · Space O(1)
function middleValue($list) {
return $list[intdiv(count($list), 2)];
}
Slow and fast pointers. · Time O(n) · Space O(1)
def middleValue(list):
return list[len(list) // 2]
Slow and fast pointers. · Time O(n) · Space O(1)
function middleValue(list: number[]): number {
return list[Math.floor(list.length / 2)];
}
Create a free account to submit solutions, track progress and save problems.
Create account