链表_leetcode234

# 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 not head :
return True

if not head.next :
return True

else :
slow = fast = head

while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next


head2 = slow.next

dummyHead = ListNode(0)
dummyHead.next = head2

p = head2.next
head2.next = None

while p:
nextP = p.next
p.next = dummyHead.next
dummyHead.next = p
p = nextP


p1 =head

head2 = dummyHead.next

p2 = head2



while p2:
if p1.val == p2.val:
p1 = p1.next
p2 = p2.next
else:
return False

return True

猜你喜欢

转载自www.cnblogs.com/lux-ace/p/10557244.html
今日推荐