Skip to content

Commit bb094ef

Browse files
committed
add 876 js solution
1 parent 1fa0e94 commit bb094ef

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
|922|[Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | |Easy|
5050
|914|[X of a Kind in a Deck of Cards](https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/) | |Easy|
5151
|905|[Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/) | |Easy|
52-
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | |Easy|
52+
|876|[Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [js](./algorithms/middleOfTheLinkedList/Solution.js) |Easy|
5353
|859|[Buddy Strings](https://leetcode.com/problems/buddy-strings/description/) | |Easy|
5454
|858|[Mirror Reflection](https://leetcode.com/problems/mirror-reflection/description/) | |Medium|
5555
|852|[Peak Index in a Mountain Array](https://leetcode.com/problems/peak-index-in-a-mountain-array/description/) | |Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var middleNode = function(head) {
13+
const list = [];
14+
const loop = (node) => {
15+
list.push(node)
16+
if (node.next) {
17+
loop(node.next)
18+
}
19+
}
20+
loop(head)
21+
const middle = list.length/2
22+
// 如果中位数是整数
23+
if (middle%1 === 0) {
24+
return list[middle];
25+
} else {
26+
return list[Math.round(middle)-1]
27+
}
28+
};

0 commit comments

Comments
 (0)