kucheasysa

Algoverse_adesh_35

Jul 4th, 2024
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. # Definition for singly-linked list.
  2. # class ListNode:
  3. #     def __init__(self, val=0, next=None):
  4. #         self.val = val
  5. #         self.next = next
  6. class Solution:
  7.     def isPalindrome(self, head: ListNode) -> bool:
  8.         slow, fast, prev = head, head, None
  9.         while fast and fast.next:
  10.             slow, fast = slow.next, fast.next.next
  11.         prev, slow, prev.next = slow, slow.next, None
  12.         while slow:
  13.             slow.next, prev, slow = prev, slow, slow.next
  14.         fast, slow = head, prev
  15.         while slow:
  16.             if fast.val != slow.val: return False
  17.             fast, slow = fast.next, slow.next
  18.         return True
Advertisement
Add Comment
Please, Sign In to add comment