Short python solution

  • 0
    class Solution(object):
        def isPalindrome(self, l):
            if not l or not
                return True
            # find the head of the second half of the list
            prev = ListNode('')
   = l
            curr =
            fast = l
            while fast and
                fast =
                curr =
                prev =
   = None
            # now curr is the head of the second half, flip the second half
            head = None
            while curr:
                tmp =
       = head
                head = curr
                curr = tmp
            # compare two lists
            first, second = l, head
            while first and second:
                if first.val != second.val:
                    return False
                first, second =,
            return True

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.