# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None
classSolution(object): definsertionSortList(self, head): """ :type head: ListNode :rtype: ListNode """ if head isNone: return head dummy = ListNode(-1) dummy.next = head current = head while current: print(current.val) begin = dummy current_next = current.next if current_next and current_next.val < current.val: tmp_current = current current.next = current_next.next while begin != tmp_current: if begin.next.val > current_next.val: current_next.next = begin.next begin.next = current_next break else: begin = begin.next else: current = current.next return dummy.next