-
Notifications
You must be signed in to change notification settings - Fork 1
/
leetcode-2130-MaximumTwinSumOfALinkedList.js
86 lines (67 loc) · 2 KB
/
leetcode-2130-MaximumTwinSumOfALinkedList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// https://leetcode.com/problems/maximum-twin-sum-of-a-linked-list/
function ListNode(val, next) {
this.val = val === undefined ? 0 : val;
this.next = next === undefined ? null : next;
}
// p: head
// r: max
// e:
// reverse ll & check max of 2 ll until the mid
var pairSum = function (head) {
// reverse ll
// <------------ A ------------ B ------------> C ------------> null
// t h
let tail = null;
const reverse = (head) => {
if (!head) return;
return reverse(head.next);
};
console.log(reverse(head));
};
// // reverse ll & check max of 2 ll until the mid
// var pairSum = function (head) {
// // reverse ll
// // <------------ A ------------ B ------------> C ------------> null
// // t c n
// let current = head.next;
// let tail = null;
// while (next) {
// head.next = tail;
// tail = head;
// head = current;
// current = current.next;
// }
// head.next = tail;
// console.log(head);
// };
const a = new ListNode(5);
const b = new ListNode(4);
const c = new ListNode(2);
const d = new ListNode(1);
a.next = b;
b.next = c;
c.next = d;
pairSum(a);
// // BFS
// var pairSum = function (head) {
// // const map = {};
// const allValues = [];
// const queue = [head];
// while (queue.length > 0) {
// const head = queue.shift();
// if (head) {
// allValues.push(head.val);
// queue.push(head.next);
// }
// }
// let max = -Infinity;
// // console.log(allValues);
// const allValuesLength = allValues.length;
// for (let i = 0; i < allValues.length / 2; i++) {
// const twinI = allValuesLength - 1 - i;
// allValues[i] = allValues[i] + allValues[twinI];
// }
// console.log(allValues);
// console.log(Math.max(...allValues));
// return Math.max(...allValues);
// };