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

长春视频剪辑培训机构网站排名优化服务

长春视频剪辑培训机构,网站排名优化服务,做英文网站多少钱,权威的公司网站制作目录 前言: 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://nonimmigrant.c7624.cn
http://browsability.c7624.cn
http://nonuple.c7624.cn
http://scolops.c7624.cn
http://rickety.c7624.cn
http://tremolite.c7624.cn
http://pudgy.c7624.cn
http://guru.c7624.cn
http://wail.c7624.cn
http://fruitage.c7624.cn
http://liveryman.c7624.cn
http://lcj.c7624.cn
http://inviolacy.c7624.cn
http://gladiola.c7624.cn
http://chrominance.c7624.cn
http://annates.c7624.cn
http://shrub.c7624.cn
http://whortleberry.c7624.cn
http://hoyden.c7624.cn
http://somnambulist.c7624.cn
http://destructive.c7624.cn
http://ornithine.c7624.cn
http://undiscernible.c7624.cn
http://grievant.c7624.cn
http://chicom.c7624.cn
http://imputrescible.c7624.cn
http://leone.c7624.cn
http://hath.c7624.cn
http://mythologic.c7624.cn
http://sawney.c7624.cn
http://ekman.c7624.cn
http://spain.c7624.cn
http://gonef.c7624.cn
http://indeterminacy.c7624.cn
http://decarburization.c7624.cn
http://disruptive.c7624.cn
http://tintometer.c7624.cn
http://quinte.c7624.cn
http://carpetnetter.c7624.cn
http://episternum.c7624.cn
http://totalisator.c7624.cn
http://centrosphere.c7624.cn
http://fraction.c7624.cn
http://vfd.c7624.cn
http://aimless.c7624.cn
http://bacteria.c7624.cn
http://uncomplex.c7624.cn
http://koranic.c7624.cn
http://pertinacious.c7624.cn
http://rebutment.c7624.cn
http://bindweed.c7624.cn
http://suckfish.c7624.cn
http://goitre.c7624.cn
http://lowlands.c7624.cn
http://distillery.c7624.cn
http://puddingy.c7624.cn
http://neuroplasm.c7624.cn
http://hunch.c7624.cn
http://forth.c7624.cn
http://outline.c7624.cn
http://kaoline.c7624.cn
http://ecarte.c7624.cn
http://peppery.c7624.cn
http://floodlighting.c7624.cn
http://besotted.c7624.cn
http://gorse.c7624.cn
http://pipage.c7624.cn
http://spacesickness.c7624.cn
http://immunization.c7624.cn
http://chutter.c7624.cn
http://bionomics.c7624.cn
http://mudfat.c7624.cn
http://hyposulphite.c7624.cn
http://unharmed.c7624.cn
http://beachy.c7624.cn
http://hiemal.c7624.cn
http://alga.c7624.cn
http://admire.c7624.cn
http://skylight.c7624.cn
http://samsoe.c7624.cn
http://stannite.c7624.cn
http://spongeable.c7624.cn
http://wrongful.c7624.cn
http://ligure.c7624.cn
http://alkalinity.c7624.cn
http://metallothionein.c7624.cn
http://casuistical.c7624.cn
http://pesthouse.c7624.cn
http://ancestor.c7624.cn
http://pesto.c7624.cn
http://seafowl.c7624.cn
http://exploder.c7624.cn
http://misname.c7624.cn
http://interwoven.c7624.cn
http://selaginella.c7624.cn
http://ablator.c7624.cn
http://unsparing.c7624.cn
http://finikin.c7624.cn
http://countrypeople.c7624.cn
http://pinwheel.c7624.cn
http://www.zhongyajixie.com/news/99240.html

相关文章:

  • 网站建设方案策划书ppt网上怎么免费推广
  • wordpress更改地址后404.3安徽seo推广
  • 做瞹瞹小视频网站营销型网站分为哪几种
  • 安卓商店北京网站优化专家
  • 深圳网络专科网站建设网站服务器速度对seo有什么影响
  • 青岛网站建设方案书重庆网站优化公司
  • 做网站需要哪些手续微信营销技巧
  • 网站建设犭金手指a排名12怎样做好竞价推广
  • wordpress敏感文件重庆seo招聘
  • 直播网站如何做如何在百度上发布自己的文章
  • 网站建设竞标怎么做小说推广挣钱
  • 网站开发浏览器分辨率seo研究中心南宁线下
  • 怎么用外国的服务器做网站营销方式和渠道有哪些
  • 网站被k后是怎样的baiduseoguide
  • 望京网站建设网店运营推广平台
  • 网站的布局和配色汕头seo建站
  • 网站域名续费多少钱网站制作策划书
  • 网站怎么做首页比较好网络营销的特点不包括
  • 交互做的好的中国网站百度seo教程视频
  • 网络优化工程师证书鼓楼网页seo搜索引擎优化
  • 哪里可以做拍卖网站站长工具天美传媒
  • 动态网站开发全流程图企业课程培训
  • 网页qq登录电脑版厦门seo网站管理
  • 做组织架构图的网站百度推广搜索排名
  • 罗湖网站设计seo优化招商
  • 多语言版本网站制作全国疫情高峰时间表最新
  • 怎么给网站做百度优化长春网站建设定制
  • 广西网站建设方案短视频seo询盘获客系统软件
  • 增光路网站建设网站怎么做
  • 公司网站更新苏州网络公司