Skip to content

Commit

Permalink
feat: complete 234
Browse files Browse the repository at this point in the history
  • Loading branch information
javaswing committed Nov 17, 2023
1 parent 6aabf80 commit 17f0c76
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions code/linked-list/palindrome-linked-list.ts
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;
};

0 comments on commit 17f0c76

Please sign in to comment.