https://oj.leetcode.com/problems/insertion-sort-list/
Insert sort to assume [0,i] is already an ordered array, the next step from [I,n] to find the smallest element to exchange to I. Use the pointer to simulate the process. Is the operation of some trouble.
Each time [Head,p] is an already ordered array, the next time from [P,tail] Find the smallest node R and its forward node L. Insert it between the lp->p. and notice whether this node is a boundary condition such as p.
/** * Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x): Val (x), Next (NULL) {} *}; */Const int INF=2147483647;CLASS Solution {Public:listnode *insertionsortlist (ListNode *head) {if (head==null ) return NULL; ListNode *p=head; ListNode *lp=null; ListNode *h=head; while (p!=null) {int minv=inf; ListNode *l,*r=p; ListNode *LQ=LP; ListNode *q=p; while (Q!=null) {if (Q->VAL<MINV) {minv=q->val; L=LQ; r=q; } lq=q; q=q->next; } if (r==p) {lp=p; p=p->next; Continue } if (L!=null) l->next=r->next; r->next=p; if (lp==null) h=r; else lp->next=r; Lp=r; }return h; }};
Leetcode-insertion sort list-List Insert sort-list operations