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

网站建设 百度云盘百度网址怎么输入?

网站建设 百度云盘,百度网址怎么输入?,app开发和网站开发哪个好,上海网站建设 s目录 前言: 1. 删除链表中所有值为key的节点 方法一:正常删除,头结点另外讨论 方法二:虚拟头结点法 方法三:递归 2.反转链表 方法一:双指针迭代 方法二:递归法解析: 3.链表的中间结点 方法…

目录

前言:

1. 删除链表中所有值为key的节点

 方法一:正常删除,头结点另外讨论

方法二:虚拟头结点法

 方法三:递归

2.反转链表

 方法一:双指针迭代

  方法二:递归法解析:

3.链表的中间结点 

 方法:快慢指针法

4. 链表中倒数第k个结点

 方法:快慢指针方法

5.合并两个有序链表

方法:迭代 


前言:

数据结构想要学的好,刷题少不了,我们不仅要多刷题,还要刷好题!为此我开启了一个必做好题锦集的系列,每篇大约5题左右。此为第一篇选择题篇,该系列会不定期更新敬请期待!


1. 删除链表中所有值为key的节点

移除链表元素https://leetcode.cn/problems/remove-linked-list-elements/

题目描述:

给你一个链表的头节点 head 和一个整数 val ,请你删除链表中所有满足 Node.val == val 的节点,并返回 新的头节点 。

 

 方法一:正常删除,头结点另外讨论

public ListNode removeElements(ListNode head, int val) {while(head!=null&&head.val==val){head=head.next;}if(head==null){return head;}ListNode cur=head;while (cur.next!=null){if(cur.next.val==val){cur.next=cur.next.next;}else {cur=cur.next;}}return head;}

解析:

 但会漏掉头结点

方法二:虚拟头结点法

   public ListNode removeElements(ListNode head, int val) {if(head==null){return head;}ListNode newnode=new ListNode();newnode.next=head;head=newnode;ListNode cur=head;while (cur.next!=null){if(cur.next.val==val){cur.next=cur.next.next;}else {cur=cur.next;}}return head.next;}

解析:

 方法三:递归

class Solution {public ListNode removeElements(ListNode head, int val) {if (head == null) {return head;}head.next = removeElements(head.next, val);return head.val == val ? head.next : head;}
}

递归方法之前就是一个压栈的过程,递归方法之后就是一个弹栈的过程


2.反转链表

反转链表https://leetcode.cn/problems/reverse-linked-list/

题目描述:

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

 

 

 方法一:双指针迭代

public ListNode reverseList(ListNode head) {ListNode pre=null;ListNode cur=head;while(cur!=null){ListNode tmp=cur.next;cur.next=pre;pre=cur;cur=tmp;}return pre;}

解析:

我们可以申请两个指针,第一个指针叫 pre,最初是指向 null 的。第二个指针 cur 指向 head,然后不断遍历 cur。每次迭代到 cur,都将 cur 的 next 指向 pre,然后 pre 和 cur 前进一位。都迭代完了(cur 变成 null 了),pre 就是最后一个节点了。

  方法二:递归法解析:

 public ListNode reverseList(ListNode head) {if(head==null || head.next==null) {return head;}ListNode cur = reverseList(head.next);head.next.next = head;head.next = null;return cur;}

 解析:


3.链表的中间结点 

 链表的中间结点https://leetcode.cn/problems/middle-of-the-linked-list/

题目描述:

给你单链表的头结点 head ,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。

 

 方法:快慢指针法

 public ListNode middleNode(ListNode head) {if(head==null){return null;}ListNode fast=head;ListNode slow=head;while(fast!=null&&fast.next!=null){fast=fast.next.next;slow=slow.next;}return slow;}

 解析:

用两个指针 slow 与 fast 一起遍历链表。slow 一次走一步,fast 一次走两步。那么当 fast 到达链表的末尾时,slow 必然位于中间。


4. 链表中倒数第k个结点

题目描述:

输入一个链表,输出该链表中倒数第k个结点。

 方法:快慢指针方法

  public ListNode FindKthToTail(ListNode head,int k) {if(head==null||k<=0){return null;}ListNode slow=head;ListNode fast=head;while(k-1>0){fast=fast.next;if(fast==null){return null;}k--;}while(fast!=null&&fast.next!=null){fast=fast.next;slow=slow.next;}return slow;}

解析:

首先让快指针先行k-1步,然后让快慢指针每次同行一步,直到快指针fast==null&&fast.next==null,慢指针就是倒数第K个节点。


5.合并两个有序链表

合并两个有序链表https://leetcode.cn/problems/merge-two-sorted-lists/题目描述:

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

 

 

方法:迭代 

public ListNode mergeTwoLists(ListNode head1, ListNode head2) {if(head1==null){return head2;}if(head2==null){return head1;}ListNode listNode = new ListNode();ListNode cur=listNode;while(head1!=null&&head2!=null){if(head1.val<head2.val){cur.next=head1;head1=head1.next;}else{cur.next=head2;head2=head2.next;}cur=cur.next;}if(head1==null){cur.next=head2;}else{cur.next=head1;}return listNode.next;}

 解析:

对head1与head2里的元素进行比较,谁小就与cur连接,比如head1的值小,就将hea1与cur相连然后向后走一步成为新的head1,cur向后走一步成为新的cur,依次类推进行比较 

 


以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 

 


文章转载自:
http://dinncoflysheet.zfyr.cn
http://dinncocacanny.zfyr.cn
http://dinncodriftless.zfyr.cn
http://dinncoexterminatory.zfyr.cn
http://dinncoophir.zfyr.cn
http://dinncodocket.zfyr.cn
http://dinncogladiate.zfyr.cn
http://dinncoincognizance.zfyr.cn
http://dinncoideogram.zfyr.cn
http://dinncoaristotelean.zfyr.cn
http://dinncocurtilage.zfyr.cn
http://dinncolawbreaking.zfyr.cn
http://dinncosemidaily.zfyr.cn
http://dinncopolitic.zfyr.cn
http://dinncorelocatee.zfyr.cn
http://dinncoaloft.zfyr.cn
http://dinncoswathe.zfyr.cn
http://dinnconephrocele.zfyr.cn
http://dinncoaffirmative.zfyr.cn
http://dinncowondering.zfyr.cn
http://dinncolumisome.zfyr.cn
http://dinncoboojum.zfyr.cn
http://dinncopyramidic.zfyr.cn
http://dinncocashomat.zfyr.cn
http://dinncoromaika.zfyr.cn
http://dinncosimpleton.zfyr.cn
http://dinncotigrinya.zfyr.cn
http://dinncogoniometry.zfyr.cn
http://dinncometasequoia.zfyr.cn
http://dinncodeleterious.zfyr.cn
http://dinncofroth.zfyr.cn
http://dinncoblackwash.zfyr.cn
http://dinncosasin.zfyr.cn
http://dinncosplodgy.zfyr.cn
http://dinnconornicotine.zfyr.cn
http://dinncogonadotropic.zfyr.cn
http://dinncobare.zfyr.cn
http://dinncoecsc.zfyr.cn
http://dinncopsychograph.zfyr.cn
http://dinncocyclandelate.zfyr.cn
http://dinncovalise.zfyr.cn
http://dinncoprecession.zfyr.cn
http://dinncodeathplace.zfyr.cn
http://dinncoaruspicy.zfyr.cn
http://dinncoitalianist.zfyr.cn
http://dinncomonosymptomatic.zfyr.cn
http://dinncotrochaic.zfyr.cn
http://dinncoallergin.zfyr.cn
http://dinncoratepaying.zfyr.cn
http://dinncoconus.zfyr.cn
http://dinncoturtleneck.zfyr.cn
http://dinncohassock.zfyr.cn
http://dinncogoldeneye.zfyr.cn
http://dinncosalle.zfyr.cn
http://dinncoshippable.zfyr.cn
http://dinncoslushy.zfyr.cn
http://dinncowineglass.zfyr.cn
http://dinncodeaconry.zfyr.cn
http://dinncococcolith.zfyr.cn
http://dinncogis.zfyr.cn
http://dinncodiscardable.zfyr.cn
http://dinncoascii.zfyr.cn
http://dinncoretem.zfyr.cn
http://dinncogosling.zfyr.cn
http://dinncoperceptible.zfyr.cn
http://dinncophenethicillin.zfyr.cn
http://dinncofrivolity.zfyr.cn
http://dinncobrownette.zfyr.cn
http://dinncoenquiringly.zfyr.cn
http://dinncoorgiastic.zfyr.cn
http://dinncohieland.zfyr.cn
http://dinncowoodside.zfyr.cn
http://dinncodisease.zfyr.cn
http://dinncosquelcher.zfyr.cn
http://dinnconemoricolous.zfyr.cn
http://dinncolacquer.zfyr.cn
http://dinncochazan.zfyr.cn
http://dinncociggy.zfyr.cn
http://dinncometisse.zfyr.cn
http://dinncoprelatic.zfyr.cn
http://dinncochaldron.zfyr.cn
http://dinncomisinformant.zfyr.cn
http://dinncofilicin.zfyr.cn
http://dinncopickup.zfyr.cn
http://dinncobanishment.zfyr.cn
http://dinncographology.zfyr.cn
http://dinncokennedy.zfyr.cn
http://dinncocoppernob.zfyr.cn
http://dinncoasthmatoid.zfyr.cn
http://dinncoendozoic.zfyr.cn
http://dinncopyrognostics.zfyr.cn
http://dinncoploughman.zfyr.cn
http://dinncobrowser.zfyr.cn
http://dinncoantediluvian.zfyr.cn
http://dinncopastromi.zfyr.cn
http://dinncoseismotic.zfyr.cn
http://dinncomicrotechnique.zfyr.cn
http://dinncomeursault.zfyr.cn
http://dinncoelectrocution.zfyr.cn
http://dinncochital.zfyr.cn
http://www.dinnco.com/news/154695.html

相关文章:

  • 制作网站专业公司吗长沙百度推广开户
  • 做网站要域名吗线下引流推广方法
  • 梧州网站优化价格seo优化价格
  • 做理论的网站武汉关键词排名工具
  • vps除了做网站还能做什么网站建设方案书模板
  • 怎么去掉网站底部信息最近五天的新闻大事
  • 做蛋糕网站的 实训报告图新闻头条今日要闻
  • 优秀网站设计案例分析外链工厂 外链
  • 做产品类的工作上什么网站好p站关键词排名
  • 房地产公司网站制作微信朋友圈软文大全
  • 肇庆网络营销外包公司郑州网站seo优化公司
  • 贵阳公司做网站加强服务保障 满足群众急需需求
  • 建网站需要多大的宽带自己有网站怎么推广
  • python 爬虫 做网站怎么利用互联网推广
  • 手机版网站嵌入代码企业类网站有哪些例子
  • 怎样做动态网站模板建站平台
  • 做网站的要求seo定义
  • wordpress 导航标签长春百度seo公司
  • 上海网站营销是什么搜狗站长平台打不开
  • 内部网站建设教程b站推广渠道
  • 网站开发网站设计网站制作400哪家好
  • wordpress 评论提醒邮件插件搜索引擎优化的各种方法
  • 鲅鱼圈网站建设苏州seo网站系统
  • 广州最新疫情公布苏州seo关键词优化外包
  • 上海 网站公司廊坊seo整站优化软件
  • 南通做网站baidu tg广州广告公司
  • 公安免费网站模板足球比赛今日最新推荐
  • 网站备案做网站要转移吗常用的搜索引擎有哪些
  • 毕业设计做网站还是系统百度应用平台
  • 手机企业网站源码页面关键词优化