-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path234.回文链表.js
68 lines (61 loc) · 1.53 KB
/
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
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
/*
* @lc app=leetcode.cn id=234 lang=javascript
*
* [234] 回文链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {boolean}
*/
var isPalindrome = function(head) {
// 方法 1 利用类似 树的 前中后续遍历,前后双指针进行比较
// let left = head;
// const traverse = (right) => {
// if (right === null) return true;
// let res = traverse(right.next);
// res = res && (left.val === right.val);
// left = left.next;
// return res;
// }
// return traverse(head)
// 方法 2 利用快慢指针
let fast = head;
let slow = head;
while (fast !== null && fast.next !== null) {
fast = fast.next.next;
slow = slow.next;
}
// 果fast指针没有指向null,说明链表长度为奇数,slow还要再前进一步:
if (fast !== null) {
slow = slow.next;
}
const reverse = (head) => {
let previousHead = null;
let current = head;
while (current !== null) {
const next = current.next;
current.next = previousHead;
previousHead = current;
current = next;
}
return previousHead;
}
// 将 slow 后面的进行反转 然后进行比较
let left = head;
let right = reverse(slow);
while (right !== null) {
if (left.val !== right.val) return false;
left = left.next;
right = right.next;
}
return true;
};
// @lc code=end