- Find the mid node of linked list
- Reverse the second half part, (slow is exactly mid one or in the first half part)
- Compare two parts
T : O(n) S : O(1)
# 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
"""
# find the mid node
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
# reverse the second-half
node = None
while slow:
nxt = slow.next
slow.next = node
node = slow
slow = nxt
# compare two part
while node # while node and head: (why head no need?)
if node.val != head.val:
return False
node = node.next
head = head.next
return True