# @param head, a ListNode # @return a ListNode definsertionSortList(self, head): ifNone == head orNone == head.next: return head
new_head = ListNode(0) new_head.next = head
last = new_head.next cur = new_head.next.next whileNone != cur: if cur.val >= last.val: last = cur cur = cur.next continue ins = new_head whileNone != ins.nextand ins.next.val <= cur.val: # stable sort ins = ins.next last.next, cur.next, ins.next, cur = cur.next, ins.next, cur, cur.next