Skip to content

Latest commit

 

History

History
24 lines (23 loc) · 577 Bytes

234.md

File metadata and controls

24 lines (23 loc) · 577 Bytes

解法一

  • 68ms
  • 73%
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def isPalindrome(self, head: ListNode) -> bool:
        rev = None
        slow = fast = head
        while fast and fast.next:
            fast = fast.next.next
            slow, rev, rev.next = slow.next, slow, rev
        if fast:
            slow = slow.next
        while rev and rev.val==slow.val:
            rev = rev.next
            slow = slow.next
        return not rev