# @lc code=start # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defisPalindrome(self, head: ListNode) -> bool: self.left = head deftraverse(right): ifnot right: returnTrue res = traverse(right.next) res = res and (self.left.val == right.val) self.left = self.left.next return res
classSolution: defisPalindrome(self, head: ListNode) -> bool: slow, fast = head, head while fast and fast.next: slow = slow.next fast = fast.next.next if fast: slow = slow.next
defreverse(head): pre = None cur = head while cur: nex = cur.next cur.next = pre pre = cur cur = nex return pre
left, right = head, reverse(slow) while right: if left.val != right.val: returnFalse left = left.next right = right.next returnTrue