Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution:
- def isPalindrome(self, head: Optional[ListNode]) -> bool:
- # Step 1: Find middle
- slow = fast = head
- while fast and fast.next:
- slow = slow.next
- fast = fast.next.next
- # Step 2: Skip middle if odd
- if fast:
- slow = slow.next
- # Step 3: Reverse second half
- prev = None
- while slow:
- nxt = slow.next
- slow.next = prev
- prev = slow
- slow = nxt
- # Step 4: Compare
- left = head
- right = prev
- while right:
- if left.val != right.val:
- return False
- left = left.next
- right = right.next
- return True
Advertisement
Add Comment
Please, Sign In to add comment