It is a linked list version of insert sorting.
Solution:
# T:O(n^2) S:O(1) class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = ListNode(0) dummy.next = head curr = head while curr.next: if curr.next.val < curr.val: pre = dummy while pre.next.val < curr.next.val: pre = pre.next tmp = curr.next curr.next = tmp.next tmp.next = pre.next pre.next = tmp else: curr = curr.next return dummy.nextRun Time: 348 ms
No comments:
Post a Comment