这道题一开始想到的比较简单。但是follow up的比较难,暂时没有想出来如何出现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
"""
if head == None:
return True
b = []
while head.next != None:
b.append(head.val)
head = head.next
b.append(head.val)
if b == b[::-1]:
return True
else:
return False