Short and Simple Python Palindrome Linked List

  • 1
    class Solution:
    # @param {ListNode} head
    # @return {boolean}
    def isPalindrome(self, head):
        node = head
        length = 0
        comp = []
        while node:
            node =
            length += 1
        for i in range(length // 2):
            if not comp[i] == comp[len(comp)-i-1]:
                return False
        return True

  • 0

    Actually, you don't need to keep track of length of the linked list in the first traversal, which saves two lines : )

  • 0

    The space complexity of your code is not constant and it's better to operate the nodes directly if possible, not translate to an array. I think it's not what the interviewer want as well.

  • 0

    if you want to use list, just do
    return comp == comp[::-1]

Log in to reply

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