class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if head is None or head.next is None:
return True
if head.next.next is None:
return head.val == head.next.val
fast = head
slow = head
p = head
# 判断中点
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
def reverse(head):
if head is None:
return head
prev = None
curr = head
while curr:
temp = curr.next
curr.next = prev
prev = curr
curr = temp
return prev
p = reverse(slow.next)
fast = head
while p:
if fast.val != p.val:
return False
fast = fast.next
p = p.next
return True
234.回文链表
于 2024-05-28 19:43:19 首次发布