跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客工具镜像开源精选AI社区
我的书AI 学习话题UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
C算法

数据结构:链表核心算法与 LeetCode 精选

总结了 LeetCode 中七道经典的链表算法题,涵盖移除元素、反转链表、查找中间节点、倒数第 k 个节点、合并有序链表、相交链表及随机链表深拷贝。通过双指针、哨兵节点等技巧,提供 C 语言代码实现与思路解析,助力掌握链表核心操作。

雾岛听风发布于 2026/3/23更新于 2026/12/2153 浏览
数据结构:链表核心算法与 LeetCode 精选

1. 移除链表元素

d489e57558fa48ac852b2823a86dffa2.png

/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* removeElements(struct ListNode* head, int val) {
    ListNode* newHead = NULL, *newTail = NULL;
    ListNode* pcur = head;
    while(pcur != NULL) {
        if(pcur->val != val) {
            if(newHead == NULL) {
                newHead = newTail = pcur;
            } else {
                newTail->next = pcur;
                newTail = newTail->next;
            }
        }
        pcur = pcur->next;
    }
    if(newTail != NULL) {
        newTail->next = NULL;
    }
    return newHead;
}

2. 反转链表

39aa95d0fe0944d18f93268d49f5a543.png

/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
 */
struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode* prev = NULL;
    struct ListNode* cur = head;
    while (cur) {
        struct ListNode* next = cur->next;
        cur->next = prev;
        prev = cur;
        cur = next;
    }
    return prev;
}

3. 链表的中间节点

f316c4e6c2dd4673b9b27bc84ad73646.png

/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
 */
struct ListNode* middleNode(struct ListNode* head) {
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    while(fast && fast->next){
        slow = slow->next;
        fast = fast->next->next;
    }
    return slow;
}

4. 返回倒数第 k 个节点

7d8c78d2f96c4b049b90362c8144706b.png

/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
 */
int kthToLast(struct ListNode* head, int k) {
    struct ListNode *fast = head, *slow = head;
    while(k--){
        fast = fast->next;
    }
    while(fast != NULL){
        slow = slow->next;
        fast = fast->next;
    }
    return slow->val;
}

5. 合并两个有序链表

33bda6c74a77458eae7845409d991cd1.png

/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    if(list1 == NULL) {
        return list2;
    }
    if(list2 == NULL) {
        return list1;
    }
    struct ListNode* l1 = list1;
    struct ListNode* l2 = list2;
    struct ListNode* newHead, *newTail;
    newHead = newTail = NULL;
    while(l1 && l2) {
        if(l1->val < l2->val) {
            if(newHead == NULL) {
                newHead = newTail = l1;
            } else {
                newTail->next = l1;
                newTail = newTail->next;
            }
            l1 = l1->next;
        } else {
            if(newHead == NULL) {
                newHead = newTail = l2;
            } else {
                newTail->next = l2;
                newTail = newTail->next;
            }
            l2 = l2->next;
        }
    }
    if(l1) {
        newTail->next = l1;
    }
    if(l2) {
        newTail->next = l2;
    }
    return newHead;
}

6. 相交链表

ec919a9804a74894a852973faae88385.png

/**
 * Definition for singly-linked list.
 * struct ListNode {
 * int val;
 * struct ListNode *next;
 * };
 */
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {
    struct ListNode* curA = headA, *curB = headB;
    int lenA = 1, lenB = 1;
    while(curA->next){
        curA = curA->next;
        ++lenA;
    }
    while(curB->next){
        curB = curB->next;
        ++lenB;
    }
    if(curA != curB){
        return NULL;
    }
    int gap = abs(lenA - lenB);
    struct ListNode* longList = headA, *shortList = headB;
    if(lenB > lenA){
        longList = headB;
        shortList = headA;
    }
    while(gap--){
        longList = longList->next;
    }
    while(longList != shortList){
        longList = longList->next;
        shortList = shortList->next;
    }
    return longList;
}

7. 随机链表的复制

1369640e4ebc41a29064d21f90ee31cf.png

53510b52c9804686aa1fe9e98b51005d.png

/**
 * Definition for a Node.
 * struct Node {
 * int val;
 * struct Node *next;
 * struct Node *random;
 * };
 */
struct Node* copyRandomList(struct Node* head) {
    struct Node* cur = head;
    while(cur){
        struct Node * copy = (struct Node*)malloc(sizeof(struct Node));
        copy->val = cur->val;
        copy->next = cur->next;
        cur->next = copy;
        cur = copy->next;
    }
    cur = head;
    while(cur){
        struct Node* copy = cur->next;
        if(cur->random == NULL){
            copy->random = NULL;
        } else{
            copy->random = cur->random->next;
        }
        cur = copy->next;
    }
    cur = head;
    struct Node* copyHead = NULL,* copyTail =NULL;
    while(cur){
        struct Node* copy = cur->next;
        if(copyTail == NULL) {
            copyHead = copyTail = copy;
        }else {
            copyTail->next = copy;
            copyTail = copyTail->next;
        }
        cur = copy->next;
    }
    return copyHead;
}

目录

  1. 1. 移除链表元素
  2. 2. 反转链表
  3. 3. 链表的中间节点
  4. 4. 返回倒数第 k 个节点
  5. 5. 合并两个有序链表
  6. 6. 相交链表
  7. 7. 随机链表的复制

更多推荐文章

查看全部
  • EnvPilot:基于 Rust 的跨平台环境变量管理工具
  • 近五年体内微/纳米机器人赋能肿瘤精准治疗综述:聚焦胶质母细胞瘤
  • C++ 基础:引用、inline 与 nullptr 用法解析
  • FDE 模式:AI 时代破解落地难题的关键解法
  • 微信公众号文章开源导出工具 wechat-article-exporter 架构剖析
  • RAG 中的融合检索与递归检索
  • 基于星辰 RPA 的小红书自动发文机器人搭建指南
  • MySQL 和 Navicat 在 Windows 上的安装与连接教程
  • C++ 哈希表核心机制:unordered 系列容器、位图与布隆过滤器实战
  • Linux 基础指令实战:压缩解压与权限管理
  • Hugging Face 访问令牌申请指南(以 Meta-Llama-3.1-8B-Instruct 为例)
  • 绿联云 NAS 配置 WebDAV 实现公网访问与同步
  • JavaScript 继承机制详解
  • 程序员转型大模型:2025 年 AI 职业发展路径分析
  • Windows 11 安装 Ubuntu 子系统
  • 鸿蒙金融理财全栈项目:基础架构、数据安全与用户体验
  • 树的基本概念与堆的功能实现
  • 2026 年十大 AI 编程工具实测与选型指南
  • 别被“会聊天”的AI骗了!真正的数字助理,应该是ToClaw这样的
  • 千笔 AI 学术写作辅助工具核心功能解析

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • Base64 字符串编码/解码

    将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online

  • Base64 文件转换器

    将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online

  • Markdown转HTML

    将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online

  • HTML转Markdown

    将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online