My python solution


  • 0
    R
    class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        prev = None
        item = []
        if not head:
            return True
        current = head
        while current:      # X->node1->node2->node3.......node7->X
            item.append(current.val)
            nnext = current.next        #save node2
            current.next = prev #let current node1 point to X
            prev = current      #let node1 be the next prev
            current = nnext      #let current be node2 for next loop
        for i in item:
            if i != prev.val:
                return False
            prev = prev.next
        return True
    

    My idea: invert the list first, and compare the reverted list with the val of the list stored in item.


Log in to reply
 

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