Skip to content

Commit 30694a1

Browse files
authored
Create 876-middle-of-the-linked-list.js
1 parent 50401df commit 30694a1

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

876-middle-of-the-linked-list.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
const middleNode = function (head) {
13+
if (head == null) return null
14+
let count = 1
15+
let iter = head
16+
while (iter.next) {
17+
iter = iter.next
18+
count++
19+
}
20+
count = (count / 2) >> 0
21+
while (count) {
22+
head = head.next
23+
count--
24+
}
25+
return head
26+
}

0 commit comments

Comments
 (0)