/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertionSortList(struct ListNode* head) {
struct ListNode *node=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* cur;
struct ListNode* tmp;
struct ListNode* pre;
struct ListNode* end;
node->next=head;
if(head!=NULL&&head->next!=NULL){
cur=head->next;
end=head;
while(cur!=NULL){
pre=node;
if (cur->val>=end->val){
end=cur;
}
else{
while(cur->val>=pre->next->val){
pre=pre->next;
}
tmp=pre->next;
end->next=cur->next;
pre->next=cur;
cur->next=tmp;
}
cur=end->next;
}
}
return node->next;
}
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertionSortList(struct ListNode* head) {
struct ListNode *node=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* cur;
struct ListNode* tmp;
struct ListNode* pre;
struct ListNode* end;
node->next=head;
if(head!=NULL&&head->next!=NULL){
cur=head->next;
end=head;
while(cur!=NULL){
pre=node;
if (cur->val>=end->val){
end=cur;
}
else{
while(cur->val>=pre->next->val){
pre=pre->next;
}
tmp=pre->next;
end->next=cur->next;
pre->next=cur;
cur->next=tmp;
}
cur=end->next;
}
}
return node->next;
}
链表插入排序实现
本文介绍了一种使用C语言在单链表上实现插入排序的方法。通过创建一个新的节点并将链表中的元素按升序重新排列,该算法有效地对链表进行排序。文中详细解释了排序过程,包括如何遍历链表并根据当前节点值调整其位置。
362

被折叠的 条评论
为什么被折叠?



