class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head or not head.next:
return True
slow = head
fast = head
while fast and fast.next:
slow=slow.next
fast=fast.next.next
slow = self.reverseList(slow)
while slow:
if slow.val != head.val:
return False
slow = slow.next
head = head.next
return True
def reverseList(self,head):
newL = None
while head:
temp = head
head = head.next
temp.next = newL
newL = temp
return newL
python leetcode 234. Palindrome Linked List
最新推荐文章于 2022-06-08 17:46:06 发布