#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *insertionSortList(ListNode *head) {
if(head==NULL||head->next==NULL) return head;
ListNode* result=NULL;
ListNode* ptr = head;
while(ptr){
ListNode* pNew = new ListNode(ptr->val);
pNew->next = NULL;
if(result==NULL){
result = pNew;
}else{
ListNode* ptr2 = result;
if(result->val>=pNew->val){
pNew->next = result;
result=pNew;
}else{
while(ptr2->next!=NULL&&ptr2->next->val<pNew->val){
ptr2=ptr2->next;
}
pNew->next=ptr2->next;
ptr2->next=pNew;
}
}
C++链表插入排序
最新推荐文章于 2022-10-18 12:56:18 发布