C语言 | Leetcode C语言题解之第147题对链表进行插入排序

题目:

题解:

cpp 复制代码
struct ListNode *insertionSortList(struct ListNode *head) {
    if (head == NULL) {
        return head;
    }
    struct ListNode *dummyHead = malloc(sizeof(struct ListNode));
    dummyHead->val = 0;
    dummyHead->next = head;
    struct ListNode *lastSorted = head;
    struct ListNode *curr = head->next;
    while (curr != NULL) {
        if (lastSorted->val <= curr->val) {
            lastSorted = lastSorted->next;
        } else {
            struct ListNode *prev = dummyHead;
            while (prev->next->val <= curr->val) {
                prev = prev->next;
            }
            lastSorted->next = curr->next;
            curr->next = prev->next;
            prev->next = curr;
        }
        curr = lastSorted->next;
    }
    return dummyHead->next;
}
相关推荐
逆境不可逃4 小时前
LeetCode 热题 100 之 64. 最小路径和 5. 最长回文子串 1143. 最长公共子序列 72. 编辑距离
算法·leetcode·动态规划
BUG_MeDe4 小时前
json格式字符串解析的简单使用 libjson-c
c语言·开发语言·json
Eward-an5 小时前
LeetCode 239. 滑动窗口最大值(详细技术解析)
python·算法·leetcode
一叶落4386 小时前
LeetCode 50. Pow(x, n)(快速幂详解 | C语言实现)
c语言·算法·leetcode
x_xbx7 小时前
LeetCode:26. 删除有序数组中的重复项
数据结构·算法·leetcode
至为芯8 小时前
IP2075_34S至为芯支持C口快充的30W功率AC/DC芯片
c语言·开发语言
j_xxx404_8 小时前
力扣困难算法精解:串联所有单词的子串与最小覆盖子串
java·开发语言·c++·算法·leetcode·哈希算法
big_rabbit05029 小时前
[算法][力扣167]Two Sum II
算法·leetcode·职场和发展
Eward-an9 小时前
LeetCode 76. 最小覆盖子串(详细技术解析)
python·算法·leetcode·职场和发展
不想看见40410 小时前
Reverse Bits位运算基础问题--力扣101算法题解笔记
笔记·算法·leetcode