Skip to content

Commit 165d300

Browse files
committed
Create 876.链表的中间结点.js
1 parent 0fdee39 commit 165d300

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

876.链表的中间结点.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+
var middleNode = function(head) {
13+
let length = 0;
14+
for (let t = head; t;) {
15+
length++;
16+
t = t.next;
17+
}
18+
19+
length = length >> 1;
20+
let result = head;
21+
while (length > 0) {
22+
result = result.next;
23+
length--;
24+
}
25+
return result;
26+
};

0 commit comments

Comments
 (0)