def isPalindrome(self, head):
if head is None or head.next is None:
return True
p = head
length = 1
while p.next is not None:
length += 1
p = p.next
num = length/2 + 1
q = head
r = q
i = 1
while i != num:
r = q
q = q.next
i += 1
if length%2 == 0:
r.next = None
r = q.next
s = None
if r is not None:
s = r.next
q.next = None
while r is not None:
r.next = q
q = r
r = s
if s is not None:
s = s.next
q = head
while p is not None:
if p.val != q.val:
return False
p = p.next
q = q.next
return True
Is my code in O(n) time and O(1) space?


yes
btw there is a more efficient way to find the midpoint and reverse the first half at the same time:https://leetcode.com/discuss/44751/11lines12withrestoreontimeo1space