-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
46 lines (39 loc) · 848 Bytes
/
index.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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function(head) {
if(!head || !head.next) return true;
let slow = head;
let fast = head;
while(fast && fast.next && fast.next.next){
fast = fast.next.next;
slow = slow.next;
}
let head2 = slow.next;
slow.next = null;
let pre = null;
while(head2){
let t = head2;
head2 = head2.next;
t.next = pre;
pre = t;
}
head2 = pre;
while(head && head2){
if(head.val === head2.val){
head = head.next;
head2 = head2.next;
}else{
return false;
}
}
return true;
};