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

【数据结构】单链表练习

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.xdnf.cn/news/9262.html

相关文章:

  • 改进系列(12):基于SAM交互式点提示的UNet腹部多脏器分割方法研究
  • 【北京盈达科技】GEO优化:引领AI时代内容霸权,重塑行业生态
  • 思澈科技助力Keep Watch Pilot 1:重新定义智能运动手表体验
  • React 虚拟dom
  • ROS2 robot控制学习(一)
  • 自然语言×数据集成新范式:SeaTunnel MCP深度解读 | 附视频讲解
  • 重新安装解决mac vscode点击不能跳转问题
  • 树莓派(Raspberry Pi)安装Docker教程
  • LabVIEW软件开发过程中如何保证软件的质量?
  • 大数据-272 Spark MLib - 基础介绍 机器学习算法 线性回归
  • openresty如何禁止海外ip访问
  • 【git】git rebase 和 git pull区别?
  • NSSCTF [NISACTF 2022]ezheap
  • 微信小程序的软件测试用例编写指南及示例--性能测试用例
  • 使用Gemini, LangChain, Gradio打造一个书籍推荐系统 (第三部分)
  • 查服务器信息 常用的一些命令 =^^ =
  • 共现矩阵的SVD降维与低维词向量计算详解
  • AI 智能体的那些事—架构设计关键点
  • 【Java实战】集合排序方法与长度获取方法辨析(易懂版)
  • 11.Java I/O 流:文件读写与数据持久化​
  • 夏季用电高峰如何防患于未“燃”?电力测温技术守护城市生命线
  • 使用 Redis 作为向量数据库
  • 5G 核心网 UE 状态深度剖析:机制、迁移与演进
  • 新版Chrome浏览器加载eDrawings 3D Viewer控件网页查看DWG、DXF
  • 利用Tushare+pyEcharts进行沪深证券数据采集与分析
  • 单向循环链表与双向链表
  • 洗鞋店干洗店线上预约管理系统;
  • 【OS安装与使用】part7-ubuntu22.04LTS 的 docker 安装与使用(实例:MTransServer服务部署)
  • AI辅助写作 从提笔难到高效创作的智能升级
  • WPF事件处理器+x名称空间