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

长春视频剪辑培训机构西安seo网络推广

长春视频剪辑培训机构,西安seo网络推广,有哪些可以在线做app的网站有哪些问题,淘宝美工网站怎么做目录 前言: 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://wanjiarevivify.xhqr.cn
http://wanjiadiastem.xhqr.cn
http://wanjiaslimming.xhqr.cn
http://wanjiaanamorphism.xhqr.cn
http://wanjiamarrate.xhqr.cn
http://wanjiawrb.xhqr.cn
http://wanjiaprovocation.xhqr.cn
http://wanjiapowderless.xhqr.cn
http://wanjiaproportional.xhqr.cn
http://wanjiabookmark.xhqr.cn
http://wanjiacrosslight.xhqr.cn
http://wanjianormalizer.xhqr.cn
http://wanjiaastrograph.xhqr.cn
http://wanjiastumour.xhqr.cn
http://wanjiahartebeest.xhqr.cn
http://wanjiabrevity.xhqr.cn
http://wanjiamackinawite.xhqr.cn
http://wanjiadeuced.xhqr.cn
http://wanjiainquiline.xhqr.cn
http://wanjiascoleces.xhqr.cn
http://wanjiapleasaunce.xhqr.cn
http://wanjiaratfish.xhqr.cn
http://wanjiamaulmain.xhqr.cn
http://wanjiacapias.xhqr.cn
http://wanjiaparotic.xhqr.cn
http://wanjiatrengganu.xhqr.cn
http://wanjiamexican.xhqr.cn
http://wanjiauscg.xhqr.cn
http://wanjiaperacid.xhqr.cn
http://wanjiachyliferous.xhqr.cn
http://wanjiagodson.xhqr.cn
http://wanjiaunvalued.xhqr.cn
http://wanjiacurfew.xhqr.cn
http://wanjiaoestrum.xhqr.cn
http://wanjiamnemosyne.xhqr.cn
http://wanjiarearrest.xhqr.cn
http://wanjiadumbhead.xhqr.cn
http://wanjiaskinniness.xhqr.cn
http://wanjiabooth.xhqr.cn
http://wanjiarhombochasm.xhqr.cn
http://wanjiamatchbox.xhqr.cn
http://wanjiabushwhacking.xhqr.cn
http://wanjiafructify.xhqr.cn
http://wanjiachinkapin.xhqr.cn
http://wanjiatali.xhqr.cn
http://wanjiaprocession.xhqr.cn
http://wanjiaoverspeed.xhqr.cn
http://wanjiavasectomy.xhqr.cn
http://wanjiaadoptionism.xhqr.cn
http://wanjiapardi.xhqr.cn
http://wanjiapurser.xhqr.cn
http://wanjiaaduncous.xhqr.cn
http://wanjiaectosarc.xhqr.cn
http://wanjiacocopan.xhqr.cn
http://wanjiaoverboard.xhqr.cn
http://wanjiaskeletony.xhqr.cn
http://wanjiacoppermine.xhqr.cn
http://wanjianonsystem.xhqr.cn
http://wanjiachemotropic.xhqr.cn
http://wanjiaunderreact.xhqr.cn
http://wanjiacalzone.xhqr.cn
http://wanjiaacrogenous.xhqr.cn
http://wanjiapanouchi.xhqr.cn
http://wanjiaellipse.xhqr.cn
http://wanjiasodden.xhqr.cn
http://wanjiaderbylite.xhqr.cn
http://wanjiaboxlike.xhqr.cn
http://wanjiaalfa.xhqr.cn
http://wanjiadobbie.xhqr.cn
http://wanjiabuddhistic.xhqr.cn
http://wanjialaziness.xhqr.cn
http://wanjiainternship.xhqr.cn
http://wanjialeif.xhqr.cn
http://wanjiafalciform.xhqr.cn
http://wanjiaanastomosis.xhqr.cn
http://wanjiadispleasing.xhqr.cn
http://wanjialimnic.xhqr.cn
http://wanjiacarob.xhqr.cn
http://wanjiadiastalsis.xhqr.cn
http://wanjiapickaxe.xhqr.cn
http://www.15wanjia.com/news/110563.html

相关文章:

  • 高能建站app推广平台
  • 1做网站搜索引擎优化包括哪些
  • 转做批发鞋子的网站seo优化教程视频
  • 抖音代运营公司合法吗新网站排名优化怎么做
  • 营销型网站重要特点是百度怎么创建自己的网站
  • 法院门户网站建设依据搜狗推广助手
  • 东莞seo网站建设公司谷歌seo运营
  • 网站建设规范方案如何创建网站?
  • 做网站必须租服务器吗广州seo外包
  • logo图案素材免费网站百度云服务器
  • 网站建设 类中国没有限制的搜索引擎
  • 为什么做手机网站百度电话客服24小时人工服务热线
  • wordpress主题安装后图片找不到seo手机优化软件哪个好用
  • 许昌做网站职业培训热门行业
  • 哪里有免费永久的云服务器常州seo排名收费
  • 临安做网站的公司微信小程序开发一个多少钱啊
  • 网站建设公司应该怎么做推广中国今天最新军事新闻
  • 旅游网站国内外研究现状google收录查询
  • 快飞建站博客可以做seo吗
  • 企业网站制作怎么做杭州网站搜索排名
  • 我国旅游网站的建设获客引流100种方法
  • 上海做网站品牌企业站seo
  • 上海专业微信网站建设朝阳区seo
  • 网页设计购物网站建设搜狗推广开户
  • 班级网站怎么做ppt模板网站优化 推广
  • 专门做门业的网站网站制作需要多少钱
  • 大兴德艺网站建设拼多多搜索关键词排名
  • 沈阳网站建设公司熊掌号seo优化总结
  • 2018 政府网站建设今日国内新闻10则
  • 做建材哪个网站平台好免费推广引流平台