234.Palindrome Linked List

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

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

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

# Definition for singly-linked list.
class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution(object):
    def isPalindrome(self, head):
        """
        :type head: ListNode
        :rtype: bool
        """
        try:
            l = []
            while head:
                l.append(head.val)
                head = head.next
            for i in range(len(l)//2+1):
                if l[i]!=l[len(l)-1-i]:
                    return False
            return True
        except:
            return False
原文地址:https://www.cnblogs.com/bernieloveslife/p/9750023.html