# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def insertionSortList(self, head: ListNode) -> ListNode:
if not head:
return head
dummyh = ListNode(0)
dummyh.next = head
lastO = head
curr = lastO.next
while curr:
if lastO.val <= curr.val:
lastO = lastO.next
else:
preO = dummyh
while preO.next.val <= curr.val:
preO = preO.next
lastO.next = curr.next
curr.next = preO.next
preO.next = curr
curr = lastO.next
return dummyh.next
?
|