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

链表OJ--上

文章目录

  • 前言
  • 一、反转链表
  • 二、移除链表元素
  • 三、链表中倒数第K个结点
  • 四、相交链表
  • 五、链表的中间结点


前言

一、反转链表

力扣206:反转链表- - -点击此处传送
在这里插入图片描述
思路图:
方法一:改变指向
在这里插入图片描述
方法二:
在这里插入图片描述
代码:

//方法一
//改变指向
struct ListNode* reverseList(struct ListNode* head) {//判断空if(head==NULL){return NULL;}struct ListNode*n1,*n2,*n3;n1=NULL;n2=head;n3=head->next;while(n2){n2->next=n1;n1=n2;n2=n3;if(n3){n3=n3->next;}}return n1;
}//方法二
//头插
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;
}

二、移除链表元素

力扣203:移除链表元素- - -点击此处传送
在这里插入图片描述
思路图:

在这里插入图片描述
方法2:
当然这题也可以使用带哨兵位的结点
在这里插入图片描述

代码

//方法1:
struct ListNode* removeElements(struct ListNode* head, int val) {struct ListNode*cur=head;struct ListNode*newhead=NULL;struct ListNode*tail=NULL;while(cur){if(cur->val!=val){if(tail==NULL){newhead=tail=cur;}else{tail->next=cur;tail=tail->next;}cur=cur->next;}else{struct ListNode*tmp=cur;cur=cur->next;free(tmp);}}if(tail)tail->next=NULL;return newhead; 
}//方法2:
struct ListNode* removeElements(struct ListNode* head, int val) {struct ListNode* cur=head;struct ListNode* tail=NULL;struct ListNode* newhead=NULL;//哨兵位newhead=tail=(struct ListNode*)malloc(sizeof(struct ListNode));while(cur){if(cur->val!=val){//尾插tail->next=cur;tail=tail->next;cur=cur->next;}else{struct ListNode*tmp=cur;cur=cur->next;free(tmp);}}tail->next=NULL;struct ListNode*tmp=newhead;newhead=newhead->next;free(tmp);return newhead;
}

三、链表中倒数第K个结点

牛客网:链表中倒数第K个结点- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
代码:

//牛客网代码模型
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) {// write code herestruct 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;
}

四、相交链表

力扣160:相交链表- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
在这里插入图片描述

代码:

//思路2:
struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) {struct ListNode*curA=headA;struct ListNode*curB=headB;int lenA=0;int lenB=0;//计算链表A的长度while(curA->next){lenA++;curA=curA->next;}//计算链表B的长度while(curB->next){lenB++;curB=curB->next;}//无交点if(curA!=curB){return NULL;}//用绝对值求出差值int n=abs(lenA-lenB);struct ListNode*longList=headA;struct ListNode*shortList=headB;//若B长if(lenB>lenA){//长链表为BlongList=headB;//短链表为AshortList=headA;}//让长链表B先走差值while(n--){longList=longList->next;}//两链表一起走while(longList!=shortList){longList=longList->next;shortList=shortList->next;}return longList;
}

五、链表的中间结点

力扣876:链表的中间结点- - -点击此处传送
在这里插入图片描述
思路图:
在这里插入图片描述
代码:

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;
}
http://www.lryc.cn/news/239982.html

相关文章:

  • 内衣洗衣机哪些品牌质量好实惠?小型洗衣机全自动
  • 推荐一款适合做智慧旅游的前端模板
  • VL06O报表添加增强字段
  • SpringBoot Admin
  • Java基础-----正则表达式
  • 基于 Eureka 的 Ribbon 负载均衡实现原理【SpringCloud 源码分析】
  • 如何用CHAT解释文章含义?
  • 创作4周年
  • 《opencv实用探索·一》QT+opencv实现图片拼接和Mat转QImage
  • Apahce虚拟主机配置演示
  • 加班做报表被嘲低效!快用大数据分析工具
  • 详解——菱形继承及菱形虚拟继承
  • 路由的控制与转发原理
  • ios qt开发要点
  • 免费小程序商城搭建之b2b2c o2o 多商家入驻商城 直播带货商城 电子商务b2b2c o2o 多商家入驻商城 直播带货商城 电子商务
  • ChatGPT最强?文心一言与ChatGPT对比
  • 算法通关村第十二关|青铜|字符串转换整数
  • CSS实现空心的“尖角”
  • 算法 全排列的应用
  • 环境配置|GitHub——如何在github上搭建自己写的网站
  • Windows系统中curl和wget命令下载说明
  • 山西电力市场日前价格预测【2023-11-24】
  • 说说你对 shell 的理解以及常见的命令?
  • 数据结构之双向带头循环链表函数功能实现与详细解析
  • SpringBoot_websocket实战
  • 香港科技大学广州|机器人与自主系统学域博士招生宣讲会—同济大学专场!!!(暨全额奖学金政策)
  • python基于GCN(图卷积神经网络模型)和LSTM(长短期记忆神经网络模型)开发构建污染物时间序列预测模型
  • SpringMVC问题
  • 【Linux】Linux的常用基本指令
  • 气候变化和人类活动对中国植被固碳的贡献量化数据月度合成产品