File tree 2 files changed +29
-1
lines changed
algorithms/middleOfTheLinkedList
2 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 49
49
| 922| [ Sort Array By Parity II] ( https://leetcode.com/problems/sort-array-by-parity-ii/ ) | | Easy|
50
50
| 914| [ X of a Kind in a Deck of Cards] ( https://leetcode.com/problems/x-of-a-kind-in-a-deck-of-cards/ ) | | Easy|
51
51
| 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|
53
53
| 859| [ Buddy Strings] ( https://leetcode.com/problems/buddy-strings/description/ ) | | Easy|
54
54
| 858| [ Mirror Reflection] ( https://leetcode.com/problems/mirror-reflection/description/ ) | | Medium|
55
55
| 852| [ Peak Index in a Mountain Array] ( https://leetcode.com/problems/peak-index-in-a-mountain-array/description/ ) | | Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments