-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0147-insertion-sort-list.cpp
43 lines (40 loc) · 1.06 KB
/
0147-insertion-sort-list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
147. Insertion Sort List
Submitted: December 20, 2024
Runtime: 43 ms (beats 5.75%)
Memory: 14.59 MB (beats 65.76%)
*/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* newHead = head;
head = head->next;
newHead->next = nullptr;
while (head != nullptr) {
cout << head->val;
ListNode* node = head;
head = head->next;
ListNode* p = newHead;
if (node->val < newHead->val) {
node->next = newHead;
newHead = node;
}
else {
for (; p->next != nullptr && p->next->val < node->val; p = p->next) ;
node->next = p->next;
p->next = node;
}
}
return newHead;
}
};