# Definition for singly-linked list.classListNode(object):def__init__(self,x):self.val=xself.next=NoneclassSolution(object):defisPalindrome(self,head):""" :type head: ListNode :rtype: bool """ifhead==Noneorhead.next==None:returnTruedummy=ListNode(-1)dummy.next=head# find the midslow=dummyfast=dummywhilefast!=Noneandfast.next!=None:slow=slow.nextfast=fast.next.nextmid=slow.nextslow.next=None# reverse the right halfstart=midend=mid.nextstart.next=Nonewhileend!=None:tmp=end.nextend.next=startstart=endend=tmp# compare the left half and right halfright_head=startleft_head=headwhileright_head!=Noneandleft_head!=None:ifright_head.val!=left_head.val:returnFalseright_head=right_head.nextleft_head=left_head.nextreturnTrue