链表的插入排序
#include<iostream>
using namespace std;
struct ListNode {
int val;
struct ListNode *next;
};
class Solution {
public:
ListNode* insertionSortList(ListNode* head) {
if (!head || !head->next)
{
return head;
}
ListNode dummy(0);
ListNode* currentNode;
ListNode* nextNode;
ListNode* pNode;
currentNode = head;
while (currentNode)
{
pNode = &dummy;
nextNode = currentNode->next;
while (pNode->next && currentNode->val > pNode->next->val)
{
pNode = pNode->next;
}
currentNode->next = pNode->next;
pNode->next = currentNode;
currentNode = nextNode;
}
return dummy.next;
}
};