class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* preNode = dummyHead;
while (preNode->next != nullptr) {
ListNode* p = dummyHead;
int val = preNode->next->val;
while (p->next != preNode->next && p->next->val <= val) {
p = p->next;
}
if (p->next == preNode->next) {
preNode = preNode->next;
} else {
ListNode* node = preNode->next;
preNode->next = preNode->next->next;
ListNode* tmp = p->next;
p->next = node;
node->next = tmp;
}
}
return dummyHead->next;
}
};
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = nullptr;
ListNode* cur = head;
ListNode* next = nullptr;
while (cur != nullptr) {
next = cur->next;
ListNode* p = dummyHead;
while (p->next != nullptr && p->next->val <= cur->val) {
p = p->next;
}
cur->next = p->next;
p->next = cur;
cur = next;
}
return dummyHead->next;
}
};