leetcode Daily Challenge on November 2nd, 2020.
leetcode-cn Daily Challenge on November 20th, 2020.
Difficulty : Medium
Related Topics : LinkedList、Sort
Sort a linked list using insertion sort.
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list. With each iteration one element (red) is removed from the input data and inserted in-place into the sorted list
Algorithm of Insertion Sort:
- Insertion sort iterates, consuming one input element each repetition, and growing a sorted output list.
- At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there.
- It repeats until no input elements remain.
Input: 4->2->1->3 Output: 1->2->3->4
Input: -1->5->3->4->0 Output: -1->0->3->4->5
- mine
- Java
-
Insert Sort
Runtime: 25 ms, faster than 67.04%, Memory Usage: 38.7 MB, less than 8.12% of Java online submissions
// O(N^2)time // O(1)space public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) return head; ListNode t = head.next; head.next = null; while (t != null) { ListNode node = t; t = t.next; node.next = null; ListNode temp = head; if (temp.val > node.val) { node.next = head; head = node; continue; } if (temp.next == null) { temp.next = node; continue; } ListNode pre = temp; ListNode cur = temp.next; while (cur != null) { if (cur.val < node.val) { if (cur.next == null) { cur.next = node; break; } else { cur = cur.next; pre = pre.next; } } else { pre.next = node; node.next = cur; break; } } } return head; }
-
Runtime: 6 ms, faster than 70.63%,Memory Usage: 39.6 MB, less than 8.12% of Java online submissions
// O(N*logN)time // O(N)space public ListNode insertionSortList(ListNode head) { if (head == null || head.next == null) return head; Map<Integer, List<ListNode>> map = new HashMap<>(); List<Integer> list = new ArrayList<>(); ListNode t = head; while (t != null) { ListNode temp = t; t = t.next; temp.next = null; List<ListNode> v = map.getOrDefault(temp.val, new ArrayList<>()); if(v.isEmpty()) list.add(temp.val); v.add(temp); map.put(temp.val, v); } Collections.sort(list); ListNode res = new ListNode(); t = res; for (int i = 0; i < list.size(); i++) { List<ListNode> temp = map.get(list.get(i)); for (int j = 0; j < temp.size(); j++) { t.next = temp.get(j); t = t.next; } } return res.next; }
-
- Java
- the most votes
Runtime: 31 ms, faster than 19.19%, Memory Usage: 38.3 MB, less than 8.12% of Java online submissions
// O(N^2)time // O(1)space public ListNode insertionSortList(ListNode head) { if (head == null) { return head; } ListNode helper = new ListNode(0); //new starter of the sorted list ListNode cur = head; //the node will be inserted ListNode pre = helper; //insert node between pre and pre.next ListNode next = null; //the next node will be inserted //not the end of input list while (cur != null) { next = cur.next; //find the right place to insert while (pre.next != null && pre.next.val < cur.val) { pre = pre.next; } //insert between pre and pre.next cur.next = pre.next; pre.next = cur; pre = helper; cur = next; } return helper.next; }
- the fastest code
- MergeSort
Runtime: 1 ms, faster than 99.53%, Memory Usage: 38.6 MB, less than 8.12% of Java online submissions
public ListNode insertionSortList(ListNode head) { // NOTE : This is mergesort. Not insertion sort if(head == null || head.next == null) { return head; } ListNode slow = head; ListNode fast = head; ListNode preslow = null; while(fast != null && fast.next != null) { preslow = slow; slow = slow.next; fast = fast.next.next; } preslow.next = null; ListNode head1 = insertionSortList(head); ListNode head2 = insertionSortList(slow); ListNode prehead = new ListNode(); ListNode current = prehead; while(head1!=null && head2 != null) { if(head1.val < head2.val){ current.next = head1; head1 = head1.next; }else{ current.next = head2; head2 = head2.next; } current = current.next; } if(head1!=null){ current.next = head1; } if(head2!=null) { current.next = head2; } ListNode result = prehead.next; prehead.next = null; return result; }