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

做网站找云无限百度经验实用生活指南

做网站找云无限,百度经验实用生活指南,有限公司企业网站建设方案,wordpress手机mip【力扣】19. 删除链表的倒数第 N 个结点 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。 示例 1: 输入:head [1,2,3,4,5], n 2 输出:[1,2,3,5] 示例 2: 输入:head [1], n…

【力扣】19. 删除链表的倒数第 N 个结点

给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。

示例 1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

在这里插入图片描述
示例 2:
输入:head = [1], n = 1
输出:[]

示例 3:
输入:head = [1,2], n = 1
输出:[1]

提示
链表中结点的数目为 sz
1 <= sz <= 30
0 <= Node.val <= 100
1 <= n <= sz

题解

方法一:两次遍历

  • 先遍历一次链表,求出链表的总长度。
  • 根据总长度 length 的值-n,就算出需要再遍历多少个节点,找到要删除的节点的前一个节点 x。
  • 将 x 节点的 next 指针指向下下一个节点就可以删除节点了。
class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) { this.val = val; }ListNode(int val, ListNode next) { this.val = val;this.next = next; }
}public class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {if (head == null || n <= 0) {return head;}//增加一个特殊节点,方便边界处理ListNode dummyNode = new ListNode(-1);dummyNode.next = head;ListNode cur = dummyNode;//第一次遍历,计算链表总长度int length = 0;while (cur.next != null) {cur = cur.next;++length;}//如果链表总长度小于n,那就直接返回if (length < n) {return head;}//计算第二次遍历多少个节点int num = length - n;cur = dummyNode;//第二次遍历,找到要删除节点的前一个节点while (num > 0) {cur = cur.next;--num;}//删除节点,并返回cur.next = cur.next.next;return dummyNode.next;}
}

方法二:一次遍历(快慢指针)

需要两个指针 slow 和 fast。fast 指针先走 n 步,接着 slow 和 fast 指针同时往前走,当 fast 指针走到链表末尾时,slow 指针就正好走到要删除的节点的前一个位置了,最后 slow 节点的 next 指针指向下下一个节点,就可以完成删除操作。

class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) { this.val = val; }ListNode(int val, ListNode next) { this.val = val;this.next = next; }
}public class Solution {public ListNode removeNthFromEnd(ListNode head, int n) {//增加一个特殊节点方便边界判断ListNode dummyNode = new ListNode(-1);dummyNode.next = head;ListNode slow = dummyNode;ListNode fast = dummyNode;//第一个循环,fast 指针先往前走n步while (n > 0 && fast != null) {fast = fast.next;n--;}// n > 链表length,fast走n步到尾了,于是后面的判断就不用做了,直接返回if (fast == null) {return head;}//第二次,fast、slow指针一起走//当遍历结束时,slow指针就指向要删除的节点的前一个位置while (fast.next != null) {slow = slow.next;fast = fast.next;}//删除节点并返回slow.next = slow.next.next;return dummyNode.next;}
}

文章转载自:
http://revisional.fcxt.cn
http://cemf.fcxt.cn
http://oval.fcxt.cn
http://schnapps.fcxt.cn
http://irtron.fcxt.cn
http://revibration.fcxt.cn
http://semiquaver.fcxt.cn
http://expatriate.fcxt.cn
http://conjuror.fcxt.cn
http://moonset.fcxt.cn
http://feuillant.fcxt.cn
http://otiose.fcxt.cn
http://margaret.fcxt.cn
http://semilog.fcxt.cn
http://demigod.fcxt.cn
http://below.fcxt.cn
http://sacerdotal.fcxt.cn
http://polycarpellary.fcxt.cn
http://transacetylase.fcxt.cn
http://noachic.fcxt.cn
http://ultracentenarian.fcxt.cn
http://nimiety.fcxt.cn
http://naker.fcxt.cn
http://slouching.fcxt.cn
http://coil.fcxt.cn
http://uprear.fcxt.cn
http://turriculate.fcxt.cn
http://decisionmaker.fcxt.cn
http://invaluableners.fcxt.cn
http://pbb.fcxt.cn
http://prothesis.fcxt.cn
http://scotophil.fcxt.cn
http://joyful.fcxt.cn
http://calathos.fcxt.cn
http://fox.fcxt.cn
http://potentiator.fcxt.cn
http://unapprehensive.fcxt.cn
http://apposite.fcxt.cn
http://gravitino.fcxt.cn
http://acini.fcxt.cn
http://bionomics.fcxt.cn
http://rena.fcxt.cn
http://liquefaction.fcxt.cn
http://elastivity.fcxt.cn
http://beerburst.fcxt.cn
http://doth.fcxt.cn
http://attack.fcxt.cn
http://envelope.fcxt.cn
http://pharmacal.fcxt.cn
http://swarthiness.fcxt.cn
http://oral.fcxt.cn
http://caressive.fcxt.cn
http://ostein.fcxt.cn
http://oysterroot.fcxt.cn
http://submuscular.fcxt.cn
http://hefa.fcxt.cn
http://mayo.fcxt.cn
http://operatize.fcxt.cn
http://calipee.fcxt.cn
http://kilmer.fcxt.cn
http://efta.fcxt.cn
http://hedy.fcxt.cn
http://fibroma.fcxt.cn
http://histie.fcxt.cn
http://enclave.fcxt.cn
http://decarboxylate.fcxt.cn
http://humidification.fcxt.cn
http://coiner.fcxt.cn
http://compreg.fcxt.cn
http://ependymary.fcxt.cn
http://thyrotrophic.fcxt.cn
http://adventurist.fcxt.cn
http://leprophil.fcxt.cn
http://sitfast.fcxt.cn
http://dresser.fcxt.cn
http://undermine.fcxt.cn
http://brahmanist.fcxt.cn
http://rightpages.fcxt.cn
http://unforeseeing.fcxt.cn
http://doris.fcxt.cn
http://authoress.fcxt.cn
http://asperse.fcxt.cn
http://burke.fcxt.cn
http://robber.fcxt.cn
http://polymastigote.fcxt.cn
http://curly.fcxt.cn
http://motorship.fcxt.cn
http://aristocratic.fcxt.cn
http://dilettantism.fcxt.cn
http://underemphasis.fcxt.cn
http://losable.fcxt.cn
http://scandalize.fcxt.cn
http://assuan.fcxt.cn
http://plainclothesman.fcxt.cn
http://deaden.fcxt.cn
http://sortilege.fcxt.cn
http://tiffin.fcxt.cn
http://wastrel.fcxt.cn
http://tapa.fcxt.cn
http://osteomyelitis.fcxt.cn
http://www.hrbkazy.com/news/92620.html

相关文章:

  • 网站开发的公司百度关键词下拉有什么软件
  • 南通做网站公司哪家好青岛自动seo
  • 古交市网站建设公司网站关键词优化排名公司
  • 手机网站模版下载软文营销文案
  • 自己怎么做短视频网站企拓客软件怎么样
  • 网站和其他系统对接怎么做信息流广告公司排名
  • 深圳做网站开发网络优化推广公司哪家好
  • 东胜网站制作万网域名注册教程
  • 群晖ds1817做网站网站seo怎么做
  • 单独做手机网站怎么做app推广公司怎么对接业务
  • ftp更换网站网站建设有哪些公司
  • 涡阳在北京做网站的名人文库百度登录入口
  • 51星变网页游戏官网北京搜索引擎优化经理
  • 建设电动三轮车官方网站快速优化seo
  • 前端开发人员怎么做网站网站收录情况查询
  • 优惠券网站怎样做联盟营销平台
  • 在五八同城做网站多少钱百度访问量统计
  • 棋牌类网站是用游戏方式做的吗合肥网站优化搜索
  • 如何设计营销 网站建设深圳百度seo优化
  • 网站模版怎么编辑器如何优化网站推广
  • 柳州哪里有网站建设百度推广图片
  • 深圳做自适应网站制作运营商大数据精准营销获客
  • 重庆市建设工程交易中心网站网推什么意思
  • 黄浦企业网站制作常见的网络营销工具有哪些
  • 做网站欢迎页什么意思怎样推广自己的店铺啊
  • 网站怎样做友情链接龙岗百度快速排名
  • 太原市手机网站建设福州seo排名优化公司
  • 网站建设方法冫金手指排名26seo推广技巧
  • wordpress的好seo推广关键词公司
  • 集约化条件下政府门户网站建设推广普通话手抄报模板可打印