题目
判断一个链表是否是回文链表
思路
找链表中点
反转后一段链表
然后依次判断两个链表值是否相等
def huiwenlink(link):
head = link.head
fast = slow = head.next
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
mid = slow
rpre = Node()
rhead_cur = mid.next
mid.next = None
while rhead_cur:
rhead_next = rhead_cur.next
rhead_cur.next = rpre
rpre = rhead_cur
result = rhead_cur
rhead_cur = rhead_next
result_head = result
while result_head.next.next is not None:
result_head = result_head.next
result_head.next = None
lresult = head.next
head_result = result
head_lresult = lresult
while head_result and head_lresult:
if head_result.value != head_lresult.value:
return False
head_lresult = head_lresult.next
head_result = head_result.next
return True