File tree Expand file tree Collapse file tree 1 file changed +38
-0
lines changed Expand file tree Collapse file tree 1 file changed +38
-0
lines changed 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 {number }
11
+ */
12
+ const pairSum = function ( head ) {
13
+ let slow = head , fast = head
14
+ while ( fast && fast . next ) {
15
+ slow = slow . next
16
+ fast = fast . next . next
17
+ }
18
+ // reverse
19
+ let next = null , pre = null
20
+ while ( slow ) {
21
+ next = slow . next
22
+ slow . next = pre
23
+ pre = slow
24
+ slow = next
25
+ }
26
+
27
+ let res = 0
28
+ while ( pre ) {
29
+ res = Math . max ( res , pre . val + head . val )
30
+ pre = pre . next
31
+ head = head . next
32
+ }
33
+
34
+ return res
35
+ } ;
36
+
37
+ // another
38
+
1
39
/**
2
40
* Definition for singly-linked list.
3
41
* function ListNode(val, next) {
You can’t perform that action at this time.
0 commit comments