We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 07f857a commit df04ab5Copy full SHA for df04ab5
Insertion_Sort_List.cc
@@ -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