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

链表OJ--下

文章目录

  • 前言
  • 一、链表分割
  • 二、环形链表I
  • 三、环形链表II
  • 四、链表的回文结构
  • 五、随机链表的复制


前言

一、链表分割

牛客网CM11:链表分割- - -点击此处传送
在这里插入图片描述
题解:
思路图:
在这里插入图片描述
代码:
在这里插入图片描述

二、环形链表I

力扣141:环形链表- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
扩展问题:
在这里插入图片描述

代码:

bool hasCycle(struct ListNode *head) {struct ListNode*fast=head,*slow=head;while(fast && fast->next){//slow走一步slow=slow->next;//fast走两步fast=fast->next->next;//若相等(相遇)则有环,返回true并退出程序if(fast==slow){return true;}}//否则无环return false;
}

三、环形链表II

力扣142:环形链表II- - -点击此处传送
在这里插入图片描述
题解:
思路图:
在这里插入图片描述
代码:

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=slow;while(head != meet){head=head->next;meet=meet->next;}return meet;}}return NULL;
}

四、链表的回文结构

牛客网OR36:链表的回文结构- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述

代码:

struct ListNode*reverseList(struct ListNode*head){struct ListNode*cur=head;struct ListNode*newhead=NULL;while(cur){struct ListNode*next=cur->next;cur->next=newhead;newhead=cur;cur=next;}return newhead;}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;}bool chkPalindrome(ListNode* head) {struct ListNode*mid=middleNode(head);struct ListNode*rhead=reverseList(mid);while(head && rhead){if(head->val != rhead->val)return false;head=head->next;rhead=rhead->next;}return true;}

五、随机链表的复制

力扣138:随机链表的复制- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
代码:

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*newhead=NULL;struct Node*tail=NULL;while(cur){struct Node*copy=cur->next;struct Node*next=copy->next;if(tail==NULL){newhead=tail=copy;}else{tail->next=copy;tail=tail->next;}cur->next=next;cur=next;}return newhead;
}
http://www.lryc.cn/news/241188.html

相关文章:

  • FreeRTOS源码阅读笔记4--semphr.h
  • 面试:MyBatis问题
  • vue中页面(路由)跳转及传值的几种方式 router-link + query + params
  • 媒体格式转换软件Permute 3 mac中文版软件特点
  • Docker实用篇
  • 开启数据库审计(db,extended级别或os级别),并将审计文件存放到/home/oracle/audit下
  • 单片机语音芯片开发要解决的问题
  • Cesium 展示——地球以及渲染数据导出(下载)为图片或 pdf
  • 大数据平台红蓝对抗 - 磨利刃,淬精兵! | 京东云技术团队
  • java游戏制作-王者荣耀游戏
  • Linux实验三:shell程序设计: shell基础
  • webpack环境变量的设置
  • 基于51单片机音乐盒设计( proteus仿真+程序+原理图+PCB+报告+讲解视频)
  • 技术分享| anyRTC之RTN网络
  • 基于GPRS的汽车碰撞自动报警系统(论文+源码)
  • qgis添加wms服务
  • 【DQN】基于pytorch的强化学习算法Demo
  • 【C++】泛型编程 ⑭ ( 类模板示例 - 数组类模板 | 容器思想 | 自定义类可拷贝 - 深拷贝与浅拷贝 | 自定义类可打印 - 左移运算符重载 )
  • 砖家测评:腾讯云标准型S5服务器和s6性能差异和租用价格
  • Linux常用命令——blkid命令
  • ES 万条以外分页检索功能实现及注意事项
  • 【MySQL】mysql中不推荐使用uuid或者雪花id作为主键的原因以及差异化对比
  • 【Unity细节】Default clip could not be found in attached animations list.(动画机报错)
  • VsCode连接远程Linux编译环境的便捷处理
  • 【UE】用样条线实现测距功能(下)
  • 矩阵知识补充
  • 机器学习之数据清洗和预处理
  • 【SpringBoot系列】SpringBoot日志配置
  • 庖丁解牛:NIO核心概念与机制详解 06 _ 连网和异步 I/O
  • 域控操作五:统一熄屏睡眠时间