Skip to content

Commit df04ab5

Browse files
committed
Create Insertion_Sort_List.cc
1 parent 07f857a commit df04ab5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Insertion_Sort_List.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
ListNode *ret = NULL, *tmp = head, *nxt = NULL;
15+
ListNode **pCur = &ret;
16+
while (tmp) {
17+
pCur = &ret;
18+
while (*pCur && (*pCur)->val <= tmp->val)
19+
pCur = &((*pCur)->next);
20+
nxt = tmp->next;
21+
tmp->next = *pCur;
22+
*pCur = tmp;
23+
tmp = nxt;
24+
}
25+
return ret;
26+
}
27+
};

0 commit comments

Comments
 (0)