234.回文链表
linked-list, https://leetcode.cn/problems/palindrome-linked-list/
给你一个单链表的头节点 head ,请你判断该链表是否为
回文链表(回文 序列是向前和向后读都相同的序列。如果是,返回 true ;否则,返回 false 。
示例 1:

输入:head = [1,2,2,1]
输出:true示例 2:

输入:head = [1,2]
输出:false提示:
- 链表中节点数目在范围
[1, 105]内 0 <= Node.val <= 9
进阶:你能否用 O(n) 时间复杂度和 O(1) 空间复杂度解决此题?
快慢指针查找链表的中间节点
python
# Definition for singly-linked list.
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return True
# 1. 使用快慢指针找到链表的中点
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# 2. 反转链表的后半部分
prev = None
while slow:
next_node = slow.next
slow.next = prev
prev = slow
slow = next_node
# 3. 对比前半部分和反转后的后半部分
left, right = head, prev
while right: # right 是反转后的链表的头
if left.val != right.val:
return False
left = left.next
right = right.next
return True递归算法:currentNode 指针是先到尾节点,由于递归的特性再从后往前进行比较。frontPointer 是递归函数外的指针。若 currentNode.val != frontPointer.val 则返回 false。反之,frontPointer 向前移动并返回 true。
算法的正确性在于递归处理节点的顺序是相反的,而我们在函数外又记录了一个变量,因此从本质上,我们同时在正向和逆向迭代匹配。
作者:力扣官方题解
python
class Solution:
def isPalindrome(self, head: ListNode) -> bool:
self.front_pointer = head
def recursively_check(current_node=head):
if current_node is not None:
if not recursively_check(current_node.next):
return False
if self.front_pointer.val != current_node.val:
return False
self.front_pointer = self.front_pointer.next
return True
return recursively_check()python
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
if not head or not head.next:
return True
# Count the length of the linked list
n = 0
cur = head
while cur:
n += 1
cur = cur.next
odd_f = n % 2 == 1
n_half = n // 2
pre = None
cur = head
cnt = 0
while cnt < n_half:
next_node = cur.next
cur.next = pre
pre = cur
cur = next_node
cnt += 1
if odd_f:
cur = cur.next # Skip the middle node if the length is odd
# Compare the reversed first half and the second half.
while cur and pre:
if cur.val != pre.val:
return False
cur = cur.next
pre = pre.next
return True
if __name__ == "__main__":
sol = Solution()
# Test case for non-palindrome linked list
head = ListNode(1, ListNode(2))
print(sol.isPalindrome(head)) # Expected output: False
# Test case for palindrome linked list
# Uncomment the following line to test a palindrome linked list
# head = ListNode(1, ListNode(2, ListNode(2, ListNode(1))))
# print(sol.isPalindrome(head)) # Expected output: True