当前位置: 首页 > article >正文

【数据结构】单链表练习

1.链表的中间节点

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

快慢指针来解决

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

2.反转链表

https://leetcode.cn/problems/reverse-linked-list/description/
三个指针 来解决
/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* reverseList(struct ListNode* head) 
{struct ListNode*n1,*n2,*n3;if(head==NULL)return NULL;n1=NULL;n2=head;n3=n2->next;while(n2){//翻转n2->next=n1;//移动n1=n2;n2=n3;if(n3)n3=n3->next;}return n1;
}

3.将两个有序链表合并为一个新的有序链表并返回

21. 合并两个有序链表 - 力扣(LeetCode)

哨兵位解决

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

4.链表分割

现有一链表的头指针 ListNode* pHead,给一定值x,编写一段代码将所有小于x的结点排在其余结点之前,且不能改变原来的数据顺序,返回重新排列后的链表的头指针。

链表分割_牛客题霸_牛客网

哨兵位解决

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:ListNode* partition(ListNode* pHead, int x) {struct ListNode*greaterGuard,*greaterTail,*lessGuard,*lessTail;greaterGuard=greaterTail=(struct ListNode*)malloc(sizeof(struct ListNode));lessGuard=lessTail=(struct ListNode*)malloc(sizeof(struct ListNode));greaterGuard->next=lessGuard->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);free(greaterGuard);return pHead;}
};

5.相交链表

160. 相交链表 - 力扣(LeetCode)

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     struct ListNode *next;* };*/
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {struct ListNode* tailA = headA, * tailB = headB;int lenA = 1, lenB = 1;// 处理空链表的情况if (headA == NULL || headB == NULL) {return NULL;}while (tailA->next){tailA = tailA->next;lenA++;}while (tailB->next){tailB = tailB->next;lenB++;}// 如果尾节点不同,则链表不相交if (tailA != tailB)return NULL;int gap = abs(lenA - lenB);struct ListNode* longlist = headA, * shortlist = headB;// 确定长链表和短链表if (lenA > lenB){longlist = headA;shortlist = headB;}else{longlist = headB;shortlist = headA;}// 长链表先走gap步while (gap--){longlist = longlist->next;}// 同步遍历找相交节点while (longlist != shortlist){longlist = longlist->next;shortlist = shortlist->next;}return longlist;
}

http://www.lryc.cn/news/2387767.html

相关文章:

  • JVM 性能优化终极指南:全版本兼容、参数公式与场景实战
  • 分布式爬虫监控架构设计
  • MySQL的参数 innodb_force_recovery 详解
  • 学习vue3:跨组件通信(provide+inject)
  • Alibaba Sentinel 入门教程:从理论到实战
  • 2.3 TypeScript 非空断言操作符(后缀 !)详解
  • 【菜狗work前端】小程序加if判断时不及时刷新 vs Web
  • 01 NLP的发展历程和挑战
  • TCP 三次握手:详解与原理
  • LabVIEW累加器标签通道
  • 在 Unity 中,Start 方法直接设置 RectTransform 的位置,时出现问题,与预计位置不匹配。
  • 永磁同步电机控制算法--IP调节器
  • Ubuntu 25.04 锁屏不能远程连接的解决方案
  • Java 自动装箱和拆箱还有包装类的缓存问题
  • java-jdk8新特性Stream流
  • 大语言模型 21 - MCP 自动操作 Figma+Cursor 实现将原型转换为代码
  • QNAP NEXTCLOUD 域名访问
  • Spring MVC深度解析:控制器与视图解析及RESTful API设计最佳实践
  • 华为OD机试真题——信道分配(2025B卷:200分)Java/python/JavaScript/C/C++/GO最佳实现
  • 比亚迪“双剑”电池获中汽中心权威认证,堪称“移动安全堡垒”。
  • 【mysql】mysql的高级函数、高级用法
  • 了解一下C#的SortedSet
  • 【平面波导外腔激光器专题系列】用于光纤传感的低噪声PLC外腔窄线宽激光器
  • Pytorch里面多任务Loss是加起来还是分别backward? | Pytorch | 深度学习
  • K8S Pod调度方法实例
  • 【mindspore系列】- 算子源码分析
  • 学习日记-day17-5.27
  • 一种比较精简的协议
  • 网络常识:网线和光纤的区别
  • OpenCV CUDA模块图像过滤------创建一个 Scharr 滤波器函数createScharrFilter()