对链表进行插入排序

对链表进行插入排序

1.题目

题目链接
在这里插入图片描述

2.解法一

在这里插入图片描述

代码:

truct ListNode* insertionSortList(struct ListNode* head){
    if(head==NULL)
    return head;
    //哨兵位
    struct ListNode* newhead=(struct ListNode*)malloc(sizeof(struct ListNode));
    newhead->next=head;
    struct ListNode* cur=head->next;
    newhead->next->next=NULL;

    while(cur)
    {
        struct ListNode* qsort=newhead->next;//取得头指针
        struct ListNode* qsort_prev=newhead;

        while(qsort)
     {
            if(cur->val<=qsort->val)//插在前面
        {
            //进行链接
            struct ListNode* next=cur->next;//保存后面的节点
            cur->next=qsort;
            qsort_prev->next=cur;
            cur=next;//更新
            break;
        }
        //排好序的链表进行后移
        qsort_prev=qsort;
        qsort=qsort->next;
     }
     if(qsort==NULL)//要插入的节点比所有节点都大
     {
        qsort_prev->next=cur;
        struct ListNode* next=cur->next;//保存后面的节点
        cur->next=NULL;//将节点中的指针置空
        cur=next;
     }

    }
      struct ListNode* ret_head=newhead->next;
      free(newhead);
      return ret_head;
};

3.解法二

在这里插入图片描述
代码

struct ListNode* insertionSortList(struct ListNode* head){
    if(head==NULL)
    return head;
    struct ListNode *newhead=(struct ListNode*)malloc(sizeof(struct ListNode));//哨兵位
    newhead->next=head;
    struct ListNode *cur=newhead->next;
    struct ListNode *prev=newhead;
    while(cur->next)
    {
        struct ListNode *next_prev=cur;
        struct ListNode *next=cur->next;       
          while(next)
       {
          if((next->val)<=(cur->val))
          {
              next_prev->next=next->next;//保存后面的节点
              prev->next=next;//插到前面
              next->next=cur;
              cur=prev->next;//cur回到起点位置
              next=next_prev->next;//next指针位置更新
          }  
          else//往后挪
          {
              next_prev=next;
              next=next->next;
        
          }
       }
       //完成一个点的比较,cur此时已经是最小的,往后挪进行下一个点的比较
           prev=cur;
           cur=cur->next;
    }
     struct ListNode *ret_head=newhead->next;
     free(newhead);
    return ret_head;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
插入排序也是一种简单有效的排序算法,对于链表的排序也可以采用插入排序算法。 C 语言实现代码如下: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 插入排序函数 struct ListNode* insertionSortList(struct ListNode* head) { if (head == NULL || head->next == NULL) { return head; } struct ListNode *newHead = (struct ListNode *)malloc(sizeof(struct ListNode)); newHead->val = -1; newHead->next = head; struct ListNode *cur = head->next, *pre = head; while (cur != NULL) { if (cur->val >= pre->val) { pre = cur; cur = cur->next; } else { struct ListNode *p = newHead; while (p->next->val < cur->val) { p = p->next; } pre->next = cur->next; cur->next = p->next; p->next = cur; cur = pre->next; } } return newHead->next; } // 创建链表 struct ListNode* createList(int arr[], int n) { struct ListNode *head = NULL, *cur = NULL; for (int i = 0; i < n; i++) { struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); node->val = arr[i]; node->next = NULL; if (head == NULL) { head = node; } else { cur->next = node; } cur = node; } return head; } // 打印链表 void printList(struct ListNode* head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int arr[] = {4, 2, 1, 3}; int n = sizeof(arr) / sizeof(int); struct ListNode *head = createList(arr, n); printf("Original list: "); printList(head); head = insertionSortList(head); printf("Sorted list: "); printList(head); return 0; } ``` 在这个代码中,我们定义了一个 `ListNode` 结构体表示链表节点,其中包含一个 `val` 字段和一个 `next` 指针指向下一个节点。 我们使用 `insertionSortList` 函数对链表进行插入排序。在函数中,我们首先创建一个新的头节点 `newHead`,将其值设为 `-1`,将其 `next` 指向原链表的头节点 `head`。 接下来,我们使用两个指针 `pre` 和 `cur` 分别指向已排序部分的末尾节点和未排序部分的头节点。如果 `cur` 的值大于等于 `pre` 的值,则将 `pre` 和 `cur` 向后移动一个节点;否则,我们需要将 `cur` 插入到已排序部分中。 为了找到 `cur` 应该插入的位置,我们使用一个指针 `p` 遍历已排序部分。如果 `p->next->val` 小于 `cur->val`,则将 `p` 向后移动一个节点;否则,我们将 `cur` 插入到 `p` 后面。 最后,我们返回 `newHead->next`,即为排序后的链表。 通过 `createList` 函数创建链表,并通过 `printList` 函数打印链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值