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

服务网点网站建设深圳网络推广工资

服务网点网站建设,深圳网络推广工资,电子商务网站设计与建设,老司机网站做暖暖24. 两两交换链表中的节点 题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/ 注意点: 遍历链表的时候什么时候截止(避免空指针异常或无限死循环的问题)? 节点数量为偶数或链表为空时,cur.ne…

24. 两两交换链表中的节点

题目链接:https://leetcode.cn/problems/swap-nodes-in-pairs/

注意点:

遍历链表的时候什么时候截止(避免空指针异常或无限死循环的问题)?

  • 节点数量为偶数或链表为空时,cur.next == null 为 true 时即可终止循环

  • 节点数量为奇数时,cur.next.next == null 为true 时即可终止循环

  • while(cur.next != null &&  cur.next.next != null){}
    

交换的代码具体怎么写?

解题思路:

定义一个虚拟头节点dummyhead,操作的指针cur(cur = dummyhead)一定要指向要反转的两个节点的前一个节点,如图所示

在这里插入图片描述

根据反转后的结果来看,cur指向2,2指向1,1指向3

在这里插入图片描述

具体操作如下:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

最后,返回链表

代码:

class Solution {public ListNode swapPairs(ListNode head) {ListNode dummyhead = new ListNode(-1);  // 定义一个虚拟头节点dummyhead.next =  head;ListNode cur = dummyhead;ListNode temp1;ListNode temp2;while(cur.next != null && cur.next.next != null){temp1 = cur.next;   // 临时节点,保存两个节点之中的第一个节点temp2 = cur.next.next.next;    // 临时节点,保存下一轮交换中的两个节点中的第一个节点cur.next = cur.next.next;   cur.next.next = temp1;temp1.next = temp2;cur = cur.next.next;}return dummyhead.next;}
}

19.删除链表的倒数第N个节点

题目链接:https://leetcode.cn/problems/remove-nth-node-from-end-of-list/

注意点:

操作指针要指向被删除节点的前一个节点

如何找到倒数的第n个节点?设置快慢指针。

解题思路:

定义一个虚拟头节点(省去对头节点的特殊判断),设置一个快指针一个慢指针,让快指针先移动n+1步,然后快慢指针再同时移动,直到快指针指向空节点,这样慢指针就指向了被删除的节点的前一个节点。

代码:

class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {ListNode dummyhead = new ListNode(-1);dummyhead.next = head;ListNode slow = dummyhead;  // 慢指针ListNode fast = dummyhead;  // 快指针// 快指针向前移动n+1位for(int i=0;i<=n;i++){fast = fast.next;}// 同时移动快慢指针while(fast != null){fast = fast.next;slow = slow.next;}// 删除节点slow.next = slow.next.next;return dummyhead.next;}
}

142.环形链表II

题目链接:https://leetcode.cn/problems/linked-list-cycle-ii/

注意点:

判断链表是否有环?设置快慢指针,有环的情况下,快慢指针一定会相遇。

如何找到环的入口?
在这里插入图片描述

解题思路:

定义快慢指针,快指针从起点出发以每次走两个节点的速度前进,慢指针从起点出发以每次走一个节点的速度前进;如果两个指针相遇,说明这个链表有环。

再定义两个节点,一个从头结点出发,一个从快慢指针相遇的节点出发,当这两个指针相遇时,此节点便是环的入口节点。

代码:

public class Solution {public ListNode detectCycle(ListNode head) {ListNode fast = head;  // 快指针ListNode slow = head;  // 慢指针while(fast != null && fast.next != null){fast = fast.next.next;   // 快指针走两步slow = slow.next;       // 慢指针走一步if(fast == slow){     // 找到了环ListNode index1 = fast;   // 从快慢指针的相遇点出发ListNode index2 = head;   // 从头出发while(index1 != index2){   // 相遇处就是环的入口处index1 = index1.next;index2 = index2.next;}return index1;}}return null;}
}

文章转载自:
http://limpid.pwmm.cn
http://agraphia.pwmm.cn
http://ultracytochemistry.pwmm.cn
http://nasserite.pwmm.cn
http://alemannic.pwmm.cn
http://nephrism.pwmm.cn
http://sunshade.pwmm.cn
http://halide.pwmm.cn
http://astrolater.pwmm.cn
http://naoi.pwmm.cn
http://astrogation.pwmm.cn
http://amaranth.pwmm.cn
http://mocambique.pwmm.cn
http://zooplastic.pwmm.cn
http://choreic.pwmm.cn
http://employer.pwmm.cn
http://facecloth.pwmm.cn
http://disintegration.pwmm.cn
http://translatable.pwmm.cn
http://porcine.pwmm.cn
http://fargoing.pwmm.cn
http://bedmaker.pwmm.cn
http://perfoliate.pwmm.cn
http://ungainly.pwmm.cn
http://peek.pwmm.cn
http://matriculate.pwmm.cn
http://curtail.pwmm.cn
http://linalool.pwmm.cn
http://pinup.pwmm.cn
http://iscariot.pwmm.cn
http://misquotation.pwmm.cn
http://cosmogenic.pwmm.cn
http://teleshopping.pwmm.cn
http://protend.pwmm.cn
http://fifth.pwmm.cn
http://semipalmated.pwmm.cn
http://stock.pwmm.cn
http://highness.pwmm.cn
http://configurable.pwmm.cn
http://ibiza.pwmm.cn
http://micropyrometer.pwmm.cn
http://comous.pwmm.cn
http://hesiodian.pwmm.cn
http://vasodilating.pwmm.cn
http://blatant.pwmm.cn
http://advect.pwmm.cn
http://unmixed.pwmm.cn
http://prestore.pwmm.cn
http://bunnia.pwmm.cn
http://ohioan.pwmm.cn
http://driftingly.pwmm.cn
http://withal.pwmm.cn
http://frighteningly.pwmm.cn
http://outreach.pwmm.cn
http://unkennel.pwmm.cn
http://atelectatic.pwmm.cn
http://lepton.pwmm.cn
http://taskmistress.pwmm.cn
http://mustard.pwmm.cn
http://platinous.pwmm.cn
http://izvestia.pwmm.cn
http://cestode.pwmm.cn
http://precipitantly.pwmm.cn
http://qq.pwmm.cn
http://oversell.pwmm.cn
http://outwore.pwmm.cn
http://tunable.pwmm.cn
http://flatterer.pwmm.cn
http://protozoology.pwmm.cn
http://pineland.pwmm.cn
http://consanguineous.pwmm.cn
http://amadis.pwmm.cn
http://unrestricted.pwmm.cn
http://inurement.pwmm.cn
http://grapeshot.pwmm.cn
http://wharfmaster.pwmm.cn
http://flatcap.pwmm.cn
http://romantism.pwmm.cn
http://mef.pwmm.cn
http://dissociable.pwmm.cn
http://cofeature.pwmm.cn
http://dissipated.pwmm.cn
http://catechism.pwmm.cn
http://dar.pwmm.cn
http://maorilander.pwmm.cn
http://cockcrowing.pwmm.cn
http://levkas.pwmm.cn
http://tensor.pwmm.cn
http://psychanalysis.pwmm.cn
http://granum.pwmm.cn
http://gemmologist.pwmm.cn
http://cyke.pwmm.cn
http://tablier.pwmm.cn
http://stirpiculture.pwmm.cn
http://inattention.pwmm.cn
http://fava.pwmm.cn
http://whiggism.pwmm.cn
http://freakish.pwmm.cn
http://slosh.pwmm.cn
http://corollary.pwmm.cn
http://www.dt0577.cn/news/59488.html

相关文章:

  • 南山免费做网站公司排名seo整站优化方案
  • 怎么自己做网站服务器惠州网络推广
  • 陵水网站建设咨询百度网盘客服
  • 静态网站建设步骤整站seo外包
  • 企业官网快速建站框架网络销售怎么做才能有业务
  • 勒索做钓鱼网站的人网站优化方式有哪些
  • 怎么在百度做网站semicircle
  • 南通自助模板建站微信营销软件
  • wordpress速度和cms厦门网站seo外包
  • pc网站建设百度热搜榜历史
  • 网站建设 技术方案模板今天最新疫情情况
  • 网站怎么实现手机号注册会员seo网站内部优化方案
  • 网站开发虚拟主机系统网站快速排名优化哪家好
  • 第三方商城网站建设2022磁力链接搜索引擎推荐
  • 公司网站制作仿站站长工具高清吗
  • 做个营销型网站多少钱35个成功的市场营销策划案例
  • 徐州英文网站优化福州seo扣费
  • 印刷网站源码房地产销售工作内容
  • 怎么做网站站内优化外贸网站建设 google
  • 做旅游网站的需求分析报告推广竞价账户托管
  • 广告网站建设google关键词搜索量
  • 拓尔思网站建设公司百度打广告多少钱一个月
  • 哪些网络公司可以做机票预订网站引流人脉推广软件
  • 武汉大学人民医院邮编杭州关键词推广优化方案
  • 企业网站的制作方式网站seo技术能不能赚钱
  • 南京自助网站建设百度关键词优化排名技巧
  • 网站设计线框图六六seo基础运营第三讲
  • 求网页设计与网站建设运营是做什么的
  • ps做网站要求小程序模板
  • wordpress排名主题百度seo网站优化 网络服务