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

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

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

雾岛听风发布于 2026/3/23更新于 2026/10/5118 浏览
数据结构:链表核心算法与 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. 随机链表的复制

更多推荐文章

查看全部
  • ClawX:OpenClaw 可视化桌面客户端入门指南
  • 基于 AI 辅助的智能在线考试系统设计与实现
  • Flutter jwt_io 鸿蒙适配指南:JWT 加解密与身份验证
  • Python FastAPI 入门指南:从零构建生产级 RESTful API
  • Visual C++ 运行库全版本一键安装工具 VisualCppRedist AIO 使用指南
  • 1Panel 部署 Ollama 与 Open WebUI 搭建私有化 AI 模型平台
  • 万方 AIGC 检测工具对比与选择指南
  • EhViewer 安卓版安装与使用全攻略:开源漫画阅读器配置指南
  • SpringAI Agent 开发实战:基于 Skills 的代码评审实践
  • 三个原生 JS 小工具:成绩评级、完数查找和数组合并去重
  • ToDesk、顺网云与海马云部署 DeepSeek 实测对比
  • 优化 PyCharm 中 Copilot 代码建议准确性的实用技巧
  • Spring Boot 缓存与性能优化
  • DeepSeek-R1-Distill-Llama-8B 在 Ollama 中的 HTTP API 鉴权与访问控制配置
  • Python 入门:30 天零基础学习规划(每日 1 小时)
  • html2canvas 核心使用场景与实战指南
  • 千笔 AI 辅助论文写作工具核心功能介绍
  • OpenClaw 对接 QQ 机器人实战:本地与云端部署指南
  • 云开发 Copilot:AI 重塑开发流程的实践指南
  • Python 办公自动化:使用 Pandas 库操作 Excel

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如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