Insertion Sort List
又是4结点互连的问题。规律就是用cur.next作为当前。
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def insertionSortList(self, head):
"""
:type head: ListNode
:rtype: ListNode
"""
if not head: return head
dummy = ListNode(0)
dummy.next = head
cur = head
while cur.next:
if cur.next.val<cur.val:
pre = dummy
while pre!=cur and pre.next.val<cur.next.val:
pre=pre.next
next = pre.next
pre.next = cur.next
cur.next = cur.next.next
pre.next.next = next
else:
cur=cur.next
return dummy.next