Leetcode 234 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.

Follow up:
Could you do it in O(n) time and O(1) space?

 O(n)空间:存成数组后比较

class Solution:

    # @param {ListNode} head

    # @return {boolean}

    def isPalindrome(self, head):

        temp = []

        while head:

            temp.append(head.val)

            head = head.next

        return temp == temp[::-1]

 

你可能感兴趣的:(LeetCode)