-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.js
58 lines (54 loc) · 1.04 KB
/
Solution.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
var isPalindrome2 = function (head) {
if (!head || !head.next) {
return true
}
let slow = head
let fast = head
while (fast !== null && fast.next !== null) {
slow = slow.next
fast = fast.next.next
}
if (fast !== null) {
slow = slow.next
}
let p = new ListNode(0)
while (slow !== null) {
let t = slow.next
slow.next = p.next
p.next = slow
slow = t
}
let left = head
let right = p.next
while (right !== null) {
if (left.val !== right.val) {
return false
}
left = left.next
right = right.next
}
return true
};
var isPalindrom3 = function (head) {
let arr = [];
let next = head;
while (next) {
arr.push(next.val);
next = next.next;
}
let len = Math.trunc(arr.length / 2);
for (let i = 0; i < len; i++) {
if (arr[i] !== arr[arr.length - 1 - i]) {
return false;
}
}
return true;
}
var isPalindrome = function (head) {
let arr = []
while (head) {
arr.push(head.val)
head = head.next
}
return arr.join('') === arr.reverse().join('')
}