1. Title
Insertion Sort List
2. Http address
https://leetcode.com/problems/insertion-sort-list/
3. The question
Sort a linked list using insertion sort.
4. My code (AC)
1 // Accepted 2 public ListNode insertionSortListTwo(ListNode head) { 3 ListNode dump = new ListNode(0); 4 ListNode cur = head; 5 ListNode tmp = null; 6 ListNode pre = null; 7 while( cur != null) 8 { 9 pre = dump; 10 while(pre.next != null && pre.next.val < cur.val) 11 pre = pre.next; 12 tmp = pre.next; 13 pre.next = cur; 14 cur = cur.next; 15 pre.next.next = tmp; 16 } 17 return dump.next; 18 }