-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* 234. 回文链表 | ||
* @param head | ||
* @returns | ||
* | ||
* @link https://leetcode.cn/problems/palindrome-linked-list/description/ | ||
* | ||
* 思路:使用快、慢指针 | ||
* 1. 快指针一次移动两个 | ||
* 2. 慢指针一次移动一个 | ||
* 在快指针到链表尾部的时候,慢指针正好在中间,如果链表奇数,则需要slow再移动一次 | ||
* | ||
* 循环中使用虚拟节点反转前半部分链表 | ||
* 之后循环反转的链表与slow剩下的节点是否相等,即可判断是否一致 | ||
*/ | ||
|
||
const isPalindromeLinked = function (head) { | ||
let fast = head, | ||
slow = head; | ||
let dumy, q; | ||
while (fast && fast.next) { | ||
q = slow; | ||
slow = slow.next; | ||
fast = fast.next.next; | ||
q.next = dumy; // 反转slow指针的链表 | ||
dumy = q; | ||
} | ||
|
||
if (fast) { | ||
// 奇数,fast.next不存在 | ||
slow = slow.next; | ||
} | ||
|
||
while (dumy && slow) { | ||
if (dumy.val !== slow.val) { | ||
return false; | ||
} | ||
dumy = dumy.next; | ||
slow = slow.next; | ||
} | ||
|
||
return true; | ||
}; |