-
Notifications
You must be signed in to change notification settings - Fork 0
/
876. 链表的中间结点.js
49 lines (45 loc) · 1022 Bytes
/
876. 链表的中间结点.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {ListNode}
*/
// 单指针法
var middleNode = function (head) {
let len = 0, midNode = head, i = 0
while (head) {
head = head.next
len++
}
// let mid = len % 2 ? Math.ceil(len / 2) : (len / 2 + 1)
let mid = len >> 1 // 位操作,右移即除以2下取整
while (++i < mid) {
midNode = midNode.next
}
return midNode
};
// 快慢指针 快指针走到终点时慢指针刚好到中点
var middleNode = function (head) {
let fast = head, slow = head
while (fast && fast.next) {
fast = fast.next.next
slow = slow.next
}
return slow
}
import { initList } from './21. 0.链表实现.js'
let list = initList()
list.push(222)
list.push(333)
list.push(444)
list.unshift(111)
// list.push(555)
// list.cycle(0)
// console.log(new Date().getTime(), list)
let res = middleNode(list.head)
console.log('----', res)