1 /**
2 * Definition for singly-linked list.
3 * struct ListNode {
4 * int val;
5 * ListNode *next;
6 * ListNode(int x) : val(x), next(NULL) {}
7 * };
8 */
9 class Solution {
10 public:
11 ListNode *insertionSortList(ListNode *head) {
12 // IMPORTANT: Please reset any member data you declared, as
13 // the same Solution instance will be reused for each test case.
14 if (head == NULL || head->next == NULL)
15 return head;
16 ListNode * itr =head->next;
17 head->next = NULL;
18 while (itr != NULL){
19 ListNode * node = head, * last =NULL;
20 ListNode *next = itr->next;
21 while ((node != NULL) && (node->val < itr->val)){
22 last = node;
23 node = node->next;
24 }
25 itr->next = node;
26 if (node == head)
27 head = itr;
28 else
29 last->next = itr;
30 itr = next;
31 }
32 return head;
33 }
34 };