-
Notifications
You must be signed in to change notification settings - Fork 0
/
234-回文链表.js
39 lines (35 loc) · 964 Bytes
/
234-回文链表.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
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function(head) {
let dummyHead = slow = fast = new ListNode();
dummyHead.next = head;
// 找中点
while(fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
let newHead = new ListNode();
newHead = slow.next;
// 将后半段链表反转
let newStart = reverse(null, newHead);
// 从头开始,和后半段新头部开始,因为没有断链,所以循环退出条件是后半段遍历完
for(let left = head; newStart != null; left = left.next, newStart = newStart.next){
if(left.val !== newStart.val) return false;
}
return true;
};
function reverse(pre, cur) {
if(!cur) return pre;
let next = cur.next;
cur.next = pre;
return reverse(cur, next);
}