Insertion Sort List, insertionsortlist
Solution:
1. This is the deformation of the sequential insertion of the linked list.
2. Set four pointers: insert, query, pre-insert, and pre-query.
1/** 2 * Definition for singly-linked list. 3 * struct ListNode {4 * int val; 5 * struct ListNode * next; 6 *}; 7 */8 struct ListNode * insertionSortList (struct ListNode * head) {9 struct ListNode * insert; 10 struct ListNode * insert_p; 11 struct ListNode * cur; 12 struct ListNode * cur_p; 13 int flag; 14 if (head = NULL) 15 return NULL; 16 insert_p = head; 17 insert = head-> next; 18 while (insert! = NULL) {19 cur = head; 20 cur_p = head; 21 flag = 0; 22 while (cur! = Insert) {23 if (head-> val> insert-> val) {// insert the header node. Note that the switching order is 24 insert_p-> next = insert-> next; 25 insert-> next = head; 26 head = insert; 27 insert = insert_p-> next; 28 flag = 1; 29 break; 30} 31 else if (cur-> val> insert-> val) {32 insert_p-> next = insert-> next; 33 cur_p-> next = insert; 34 insert-> next = cur; 35 insert = insert_p-> next; 36 flag = 1; 37 break; 38} 39 cur_p = cur; 40 cur = cur-> next; 41} 42 if (flag = 0) {// flag is used to check whether the insert operation is performed. if the insert operation has been moved to the next position, no insert operation is required; 44 insert = insert-> next; 45} 46} 47 return head; 48}