链表排序(采用插入排序)
# 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 head == None:
return None
h = ListNode(-1)
h.next = head
pre = h.next
cur = pre.next
while cur != None:
if pre.val > cur.val:
ppre = h
p = ppre.next
while p != cur and p.val < cur.val:
ppre = ppre.next
p = p.next
pre.next = cur.next
cur.next = ppre.next
ppre.next = cur
cur = pre.next
else:
cur = cur.next
pre = pre.next
return h.next