链表OJ归纳总结 ------- C语言

article/2025/8/25 14:18:40

一、移除链表元素 

OJ链接https://leetcode.cn/problems/remove-linked-list-elements/submissions/

 1.1. | 解题思路 |

创建一个新的哨兵头节点 guard,创建尾节点 tail,创建 cur 用于遍历原链表数据。

对原链表进行遍历,若 cur->val != val,则将 cur 尾插到 tail 的后面,最后将 guard -> next 作为返回值。

1.2. | 题解代码 | 

struct ListNode* removeElements(struct ListNode* head, int val){struct ListNode* guard=(struct ListNode*)malloc(sizeof(struct ListNode));struct ListNode* tail=guard;struct ListNode* cur=head;while(cur){if(cur->val!=val){tail->next=cur;tail=tail->next;}cur=cur->next;}tail->next=NULL;head=guard->next;free(guard);guard=NULL;return head;
}

二、反转链表

OJ链接https://leetcode.cn/problems/reverse-linked-list/description/

2.1. | 解题思路 |

创建变量 prevcurnext。用 cur 遍历链表,将 cur->next 指向 prev 从而达到反转链表的效果。

2.2. | 题解代码 |

struct ListNode* reverseList(struct ListNode* head){struct ListNode* cur=head;struct ListNode* prev=NULL;struct ListNode* next=NULL;while(cur){next=cur->next;cur->next=prev;prev=cur;cur=next;}return prev;
}

三、链表的中间结点

OJ链接https://leetcode.cn/problems/middle-of-the-linked-list/description/

3.1. | 解题思路 |

利用快慢指针解决此题正合适,慢指针走一步,快指针走两步。最后快指针走完时,慢指针的位置恰好在链表中间结点处。

注:此题要分两种情况讨论,① 链表长度为奇数,② 链表长度为偶数

| 奇数 |

| 偶数 |

注:链表长度为奇数和链表长度为偶数的结束条件不一样,需要分情况讨论

3.2. | 题解代码 |

struct ListNode* middleNode(struct ListNode* head){struct ListNode* fast=head;struct ListNode* slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;}return slow;
}

四、链表中倒数第k个结点

OJ链接https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&&tqId=11167&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking

4.1. | 解题思路 |

创建一个快指针一个慢指针,先让快指针走k步,再让快指针和慢指针同时走,一次走一步,当快指针指向NULL时,慢指针指向的就是链表中倒数第k个结点。

4.2. | 题解代码 |

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {struct ListNode* fast=pListHead;struct ListNode* slow=pListHead;while(k--){if(fast==NULL){return NULL;}fast=fast->next;}while(fast){fast=fast->next;slow=slow->next;}return slow;
}

五、合并两个有序链表

OJ链接https://leetcode.cn/problems/merge-two-sorted-lists/description/

5.1. | 解题思路 |

创建一个新的带头结点的链表,创建指针cur1指向链表1,创建指针cur2指向链表2,对比指针cur1指向结点的数据和指针cur2指向结点的数据,将小的那个尾插在新链表后,当指针cur1和指针cur2其中一个指向NULL时,说明其中一个链表已经结束,将另一个未结束的链表直接尾插在新链表后即可。

5.2. | 题解代码 |

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){struct ListNode* guard=(struct ListNode*)malloc(sizeof(struct ListNode));guard->next=NULL;struct ListNode* tail=guard;struct ListNode* cur1=list1;struct ListNode* cur2=list2;while(cur1&&cur2){if(cur1->val<cur2->val){tail->next=cur1;cur1=cur1->next;}else{tail->next=cur2;cur2=cur2->next;}tail=tail->next;}if(cur1){tail->next=cur1;}if(cur2){tail->next=cur2;}struct ListNode* head=guard->next;free(guard);guard=NULL;return head;
}

六、链表分割

OJ链接https://www.nowcoder.com/practice/0e27e0b064de4eacac178676ef9c9d70?tpId=8&&tqId=11004&rp=2&ru=/activity/oj&qru=/ta/cracking-the-coding-interview/question-ranking

6.1. | 解题思路 |

创建两个新的带头结点的链表,一个链表的头结点是lessGuard,另一个链表的头结点是greaterGuard。创建指针cur遍历原链表,将原链表中结点的数值小于x的尾插在lessGuard后,将原链表中结点的数值大于x的尾插在greaterGuard后,最后让lessTail->next指向greaterGuard->next,再让greaterTail->next指向NULL。

6.2. | 题解代码 |

class Partition {
public:ListNode* partition(ListNode* pHead, int x) {struct ListNode* lessGuard,*greaterGuard,*lessTail,*greaterTail;lessGuard=lessTail=(struct ListNode*)malloc(sizeof(struct ListNode));greaterGuard=greaterTail=(struct ListNode*)malloc(sizeof(struct ListNode));lessGuard->next=NULL;greaterGuard->next=NULL;struct ListNode* cur=pHead;while(cur){if(cur->val<x){lessTail->next=cur;lessTail=lessTail->next;}else{greaterTail->next=cur;greaterTail=greaterTail->next;}cur=cur->next;}lessTail->next=greaterGuard->next;greaterTail->next=NULL;pHead=lessGuard->next;free(lessGuard);lessGuard=NULL;free(greaterGuard);greaterGuard=NULL;return pHead;}
};

七、链表的回文结构

OJ链接https://www.nowcoder.com/practice/d281619e4b3e4a60a2cc66ea32855bfa?tpId=49&&tqId=29370&rp=1&ru=/activity/oj&qru=/ta/2016test/question-ranking

7.1. | 解题思路 |

首先在链表中找到中间结点,然后反转中间结点以后的所有结点(包括中间结点),利用指针head和指针rmid遍历链表,遍历过程中若存在head->val != rmid->val,则该链表不属于回文结构,return false;若将链表遍历完全都没有return false,则该链表属于回文结构,return true。注:遍历链表的结束条件因链表的长度是奇是偶而不同 

7.2. | 题解代码 |

class PalindromeList {
public:struct ListNode* reverseList(struct ListNode* head){struct ListNode* cur=head;struct ListNode* prev=NULL;struct ListNode* next=NULL;while(cur){next=cur->next;cur->next=prev;prev=cur;cur=next;}return prev;
}struct ListNode* middleNode(struct ListNode* head){struct ListNode* fast=head;struct ListNode* slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;}return slow;
}bool chkPalindrome(ListNode* head) {struct ListNode* mid=middleNode(head);struct ListNode* rmid=reverseList(mid);while(head&&rmid){if(head->val!=rmid->val){return false;}head=head->next;rmid=rmid->next;}return true;}
};

八、相交链表

OJ链接https://leetcode.cn/problems/intersection-of-two-linked-lists/description/

8.1. | 解题思路 |

首先分别遍历两个链表记录链表的长度,再判断两个链表的尾结点地址是否相等,相等则两链表相交,不相等则不相交。创建指针curA和指针curB,让长的链表的cur先走gap步(gap=abs(lenA-lenB)),再让两个链表同时走,每次走一步,当第一次curA==curB,此时的结点为两个链表的相交结点。

8.2. | 题解代码 |

struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {if(headA==NULL||headB==NULL){return NULL;}struct ListNode* curA=headA;struct ListNode* curB=headB;int lenA=1;while(curA->next){lenA++;curA=curA->next;}int lenB=1;while(curB->next){lenB++;curB=curB->next;}if(curA!=curB){return NULL;}int gap=abs(lenA-lenB);struct ListNode* longList=headA;struct ListNode* shortList=headB;if(lenA<lenB){longList=headB;shortList=headA;}while(gap--){longList=longList->next;}while(longList!=shortList){longList=longList->next;shortList=shortList->next;}return longList;
}

九、判断链表是否带环

OJ链接https://leetcode.cn/problems/linked-list-cycle/description/

9.1. | 解题思路 |

创建一个快指针fast,一个慢指针slow,fast一次走两步,slow一次走一步。若链表带环,当slow进环时,fast已经在环中了,此时变为fast追逐slow,当fast追上slow时,表明链表带环;若链表不带环,则fast最终会指向NULL或fast->next指向NULL(取决于链表的长度是奇数还是偶数)。

9.2. | 题解代码 |

bool hasCycle(struct ListNode *head) {struct ListNode* fast=head;struct ListNode* slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;if(fast==slow){return true;}}return false;
}

十、判断链表是否带环 + 返回入环的第一个结点

OJ链接https://leetcode.cn/problems/linked-list-cycle-ii/description/

10.1. | 解题思路 |

| 方法一:公式推导法 |

| 方法二:链表相交结点法 |

10.2. | 题解代码 |

//方法一:公式推导法struct ListNode *detectCycle(struct ListNode *head) {struct ListNode* fast=head;struct ListNode* slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;if(fast==slow){struct ListNode* meet=fast;while(meet!=head){meet=meet->next;head=head->next;}return meet;}}return NULL;
}//方法二:链表相交结点法struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {if(headA==NULL||headB==NULL){return NULL;}struct ListNode* curA=headA;struct ListNode* curB=headB;int lenA=1;while(curA->next){lenA++;curA=curA->next;}int lenB=1;while(curB->next){lenB++;curB=curB->next;}if(curA!=curB){return NULL;}int gap=abs(lenA-lenB);struct ListNode* longList=headA;struct ListNode* shortList=headB;if(lenA<lenB){longList=headB;shortList=headA;}while(gap--){longList=longList->next;}while(longList!=shortList){longList=longList->next;shortList=shortList->next;}return longList;
}struct ListNode *detectCycle(struct ListNode *head) {struct ListNode* fast=head;struct ListNode* slow=head;while(fast&&fast->next){slow=slow->next;fast=fast->next->next;if(fast==slow){struct ListNode* meet=fast;struct ListNode* next=meet->next;meet->next=NULL;struct ListNode* entryNode=getIntersectionNode(head,next);meet->next=next;return entryNode;}}return NULL;
}

十一、复制带随机指针的链表

OJ链接https://leetcode.cn/problems/copy-list-with-random-pointer/description/

11.1. | 解题思路 |

① 复制结点,链接到原链表中

② 更新copy结点的random

③ 将copy结点解下,链接成新的链表

     创建一个新的带头结点的链表将所有的copy结点尾插在新头结点后

 还原原链表

11.2. | 题解代码 |

struct Node* copyRandomList(struct Node* head) {struct Node* cur=head;struct Node* next=NULL;struct Node* copy=NULL;//1.复制节点,链接到原链表中while(cur){next=cur->next;copy=(struct Node*)malloc(sizeof(struct Node));copy->val=cur->val;cur->next=copy;copy->next=next;cur=next;}//2.更新copy节点的randomcur=head;while(cur){copy=cur->next;if(cur->random==NULL){copy->random=NULL;}else{copy->random=cur->random->next;}cur=cur->next->next;}//3.将copy节点解下,重新链接成新链表,将原链表还原struct Node* copyHead=(struct Node*)malloc(sizeof(struct Node));copyHead->next=NULL;struct Node* copyTail=copyHead;cur=head;while(cur){copy=cur->next;next=copy->next;copyTail->next=copy;copyTail=copyTail->next;cur->next=next;cur=cur->next;}struct Node* newHead=copyHead->next;free(copyHead);copyHead=NULL;return newHead;
}

http://chatgpt.dhexx.cn/article/wNMJY20S.shtml

相关文章

【链表复习】C++ 链表复习及题目解析 (2)

目录 牛客 CM11 链表分割 牛客 OR36 之链表的回文结构 Leetcode 160. 相交链表 LeetCode 141. 环形链表 LeetCode 138. 复制带随机指针的链表 本文继续延续前文&#xff0c;为大家带来几道经典的链表中等难度的题目。 牛客 CM11 链表分割 现有一链表的头指针 ListNode* p…

【链表OJ题(三)】链表中倒数第k个结点

​ ​&#x1f4dd;个人主页&#xff1a;Sherry的成长之路 &#x1f3e0;学习社区&#xff1a;Sherry的成长之路&#xff08;个人社区&#xff09; &#x1f4d6;专栏链接&#xff1a;数据结构 &#x1f3af;长路漫漫浩浩&#xff0c;万事皆有期待 文章目录 链表OJ题(三)1. 链表…

【20230205】链表小结

链表&#xff08;list&#xff09; 链表是一种通过指针串联在一起的线性结构&#xff0c;每一个节点由两部分组成&#xff0c;一个是数据域一个是指针域&#xff08;存放指向下一个节点的指针&#xff09;&#xff0c;最后一个节点的指针域指向null&#xff0c;链表的入口节点称…

【链表复习】C++ 链表复习及题目解析 (3)

目录 剑指offer 中的链表题目 JZ6 从尾到头打印链表 JZ18 删除链表的结点 JZ24 反转链表 JZ25 合并两个排序的链表 JZ52 两个链表的第一个公共结点 JZ23 链表中环的入口结点 JZ22 链表中倒数第k 个结点 JZ35 复杂链表的复制 JZ76 删除链表中重复的结点 本次给大家带来…

【023】C/C++数据结构之链表及其实战应用

C 链表及其实战应用 引言一、链表的概述二、利用链表设计一个学生管理系统2.1、设计主函数main()2.2、实现插入节点2.3、实现链表的遍历2.4、实现链表的查找2.5、实现删除某个节点2.6、实现释放链表2.7、完整代码 总结 引言 &#x1f4a1; 作者简介&#xff1a;专注于C/C高性能…

KNN分类算法详解

参考&#xff1a;https://www.cnblogs.com/listenfwind/p/10311496.html https://www.cnblogs.com/listenfwind/p/10685192.html 1. 概述 KNN 可以说是最简单的分类算法之一&#xff0c;同时&#xff0c;它也是最常用的分类算法之一。注意&#xff1a;KNN 算法是有监督学习中的…

【python代码实现】朴素贝叶斯分类算法

目录 前置知识1、概念2、算法原理2.1、条件概率2.2、全概率2.3、先验概率2.4、后验概率 朴素贝叶斯分类算法1、构建数据集2、分类概率3、条件概率4、先验概率 前置知识 1、概念 上一篇我们讲到的决策树算法&#xff0c;是反映了一种非常明确、固定的判断选择过程&#xff0c;…

分类算法-KNN(原理+代码+结果)

KNN&#xff0c;即K最邻近算法&#xff0c;是数据挖掘分类技术中比较简单的方法之一&#xff0c;简单来说&#xff0c;就是根据“最邻近”这一特征对样本进行分类。 1、K-means和KNN区别 K-means是一种比较经典的聚类算法&#xff0c;本质上是无监督学习&#xff0c;而KNN是分…

伯努利贝叶斯分类算法

贝叶斯分类的核心概念&#xff1a; 我们对某件事情的判断首先有一个概率&#xff0c;这个概率称为先验概率。先验概率时根据经验总结出来的概率值&#xff0c;如果首先没有经验&#xff0c;那么可以将先验概率设置为50%&#xff0c;随着后面事情的发展&#xff0c;再调整先验概…

【机器学习原理】KNN分类算法

上一篇&#xff1a;Logistic回归分类算法 文章目录 一、KNN分类算法&#xff1a;用多数表决进行分类1. 用“同类相吸”的办法解决分类问题可视化下的分类问题 2. KNN分类算法的基本方法&#xff1a;多数表决3. 表决权问题4. KNN的具体含义 KNN分类算法原理1. KNN分类算法的基本…

Python实现分类算法

前言&#xff1a;出自于学校课程数据挖掘与分析布置的实验小作业&#xff0c;案例经典&#xff0c;代码注释较全&#xff0c;供大家参考。 题目&#xff1a; 现有西瓜挑选数据文件&#xff1a;dataset.txt&#xff0c;编程实现朴素贝叶斯算法&#xff0c;并判断有如下特征的瓜…

贝叶斯分类算法

贝叶斯分类是一类分类算法的总称&#xff0c;这类算法均以贝叶斯定理为基础&#xff0c;故统称为贝叶斯分类。而朴素朴素贝叶斯分类是贝叶斯分类中最简单&#xff0c;也是常见的一种分类方法。这篇文章我尽可能用直白的话语总结一下我们学习会上讲到的朴素贝叶斯分类算法&#…

基于Python实现五大常用分类算法(原理+代码)

读&#xff1a; 在机器学习和统计中&#xff0c;分类算法通过对已知类别训练集的计算和分析&#xff0c;从中发现类别规则并预测新数据的类别。分类被认为是监督学习的一个实例&#xff0c;即学习可以获得正确识别的观察的训练集的情况。 实现分类的算法&#xff0c;特别是在具…

EIGRP综合实验解析

实验要求 1.R1为ISP,只能配置IP地址 2.R1与R2之间为PPP封装&#xff0c;使用CHAP认证&#xff0c;R1为主认证方 3.R2-R8地址为172.16.0.0/16 4.R4的S1/1口带宽为800K。R4到R2环回为非等开销负载均衡 5.保证更新安全&#xff0c;减少路由条目数量 6.R6到达R8环回通过R7进行 7.R2…

EIGRP协议

EIGRP是距离矢量路由协议&#xff0c;但又非距离矢量那样路由完全是别人告诉&#xff0c;而是通过维护3张表自己对比计算后放入路由表。同样会受水平分割影响。 EIGRP建邻居过程 第一步&#xff1a;路由器R1和R2接口配置EIGRP后&#xff0c;在相应接口上向外组播发送Hello包…

EIGRP总结

EIGRP 增强内部网关路由协议 无类别距离矢量IGP协议&#xff1b; 增量更新—仅触发更新&#xff0c;无周期更新----更新量小&#xff08;DV&#xff09;&#xff0c;可靠性高&#xff08;RTP&#xff09;&#xff0c;保活机制&#xff08;hello&#xff09; 复合度量—多个参数…

EIGRP

EIGRP增强型网关路由协议 基本内容&#xff1a; Cisco私有&#xff1b;无类别距离矢量协议&#xff1b;跨层封装协议&#xff0c;封装于网络层–协议号88&#xff1b;组播更新&#xff1a;224.0.0.10 &#xff1b;支持非等开销负载均衡&#xff1b;增量更新&#xff08;部分更…

思科EIGRP配置及基本讲解

思科EIGRP配置及基本讲解 什么是EIGRP EIGRP全称 [增强型内部网关路由选择协议] 是思科自主研发的动态路由选择协议&#xff0c;按类型划分是一款IGP协议&#xff0c;距离矢量协议&#xff0c;是一款基于传闻的协议。 EIGRP是思科的私有协议&#xff0c;直到13年&#xff0c;此…

EIGRP协议的配置

EIGRP(Enhanced Interior Gateway Routing Protocol&#xff0c;增强型内部网关路由协议)是Cisco公司开发的一个平衡混合型路由协议&#xff0c;它融合了距离向量和链路状态两种路由协议的优点&#xff0c;支持IP&#xff0c;IPX和ApplleTalk等多种网络层协议。由于TCP/IP是当今…

网络篇 EIGRP协议-27

目录 一、EIGRP的基本概述 二、EIGRP的特点 三、EIGRP的四种重要技术 四、EIGRP的相关术语 五、EIGRP的三张表 1.路由表 2.邻居表 3.拓扑表 六、EIGRP的五个分组 1.Hello分组&#xff1a; 2.Update更新分组&#xff1a; 3.Query查询分组&#xff1a; 4.Reply应答分…