给定链表L0->L1->…L(n-1)->Ln,把它重新排序为L0->Ln->L1->L(n-1)->L2->L(n-2)…要求(1)在原来链表基础上进行排序,不能申请新节点(2)只能修改节点next域,不能修改数据域
class LNode:
def __init__(self,x=None):
self.data=x
self.next=None
def FindMiddleNode(head):
#查找链表中间节点并断开
if head is None or head.next is None:
slow_next = head
return slow_next
else:
slow = head
fast = head
slow_next = head
while fast is not None and fast.next is not None:
slow = slow.next #慢指针走一步
fast = fast.next.next#快指针走两步
slow_next = slow.next
slow.next = None #断开指针
return slow_next
def Reverse(head):
#对不带头节点的单链表翻转
if head is None or head.next is None:
return head
pre = head
cur = head.next
pre.next = None
while cur is not None:
next = cur.next
cur.next &