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

河南省建设工程造价协会网站网站制作步骤流程图

河南省建设工程造价协会网站,网站制作步骤流程图,网站建设与推广的销售,搭建钓鱼网站教程剑指 Offer 22. 链表中倒数第k个节点 难度:easy\color{Green}{easy}easy 题目描述 输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。 例如,一个链…

剑指 Offer 22. 链表中倒数第k个节点

难度:easy\color{Green}{easy}easy


题目描述

输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。

例如,一个链表有 666 个节点,从头节点开始,它们的值依次是 1、2、3、4、5、61、2、3、4、5、6123456。这个链表的倒数第 333 个节点是值为 444 的节点。

示例:

给定一个链表: 1->2->3->4->5, 和 k = 2.返回链表 4->5.

算法

(直接遍历)

最简单直接的方法即为顺序查找,假设当前链表的长度为 n,则我们知道链表的倒数第 k 个节点即为正数第 n−k 个节点,此时我们只需要顺序遍历到链表的第 n−k 个节点即为倒数第 k 个节点。

我们首先求出链表的长度 n,然后顺序遍历到链表的第 n−k 个节点返回即可。

复杂度分析

  • 时间复杂度O(n)O(n)O(n),其中 nnn 是链表的长度。最坏需要遍历链表两次。

  • 空间复杂度 : O(1)O(1)O(1)

C++ 代码

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* getKthFromEnd(ListNode* head, int k) {int n = 0;for (auto p = head; p; p = p->next) n ++;auto dummy = new ListNode(-1);dummy->next = head;for (int i = 0; i < n - k + 1; i ++) {dummy = dummy->next;}return dummy;}
};


文章转载自:
http://dinncorelish.ssfq.cn
http://dinncofoxed.ssfq.cn
http://dinncoanomalistic.ssfq.cn
http://dinncoinnovatory.ssfq.cn
http://dinncomangabey.ssfq.cn
http://dinncopleuroperitoneal.ssfq.cn
http://dinncocasefy.ssfq.cn
http://dinncodiascope.ssfq.cn
http://dinnconextel.ssfq.cn
http://dinncogranduncle.ssfq.cn
http://dinncochancery.ssfq.cn
http://dinncoautoptic.ssfq.cn
http://dinncochangeless.ssfq.cn
http://dinncoforepale.ssfq.cn
http://dinncosupply.ssfq.cn
http://dinncoflench.ssfq.cn
http://dinncoreoppose.ssfq.cn
http://dinncoqueensware.ssfq.cn
http://dinncojadeite.ssfq.cn
http://dinncopresumably.ssfq.cn
http://dinncodjakarta.ssfq.cn
http://dinncocoy.ssfq.cn
http://dinncooverweening.ssfq.cn
http://dinncowomankind.ssfq.cn
http://dinncodecagynous.ssfq.cn
http://dinncoequanimousness.ssfq.cn
http://dinncoglengarry.ssfq.cn
http://dinncocleared.ssfq.cn
http://dinncoprincock.ssfq.cn
http://dinncojvc.ssfq.cn
http://dinncotautophony.ssfq.cn
http://dinncocharm.ssfq.cn
http://dinncogaza.ssfq.cn
http://dinncofeatly.ssfq.cn
http://dinncogumboil.ssfq.cn
http://dinncopronator.ssfq.cn
http://dinncogummite.ssfq.cn
http://dinncoothergates.ssfq.cn
http://dinncoheadspring.ssfq.cn
http://dinncobaume.ssfq.cn
http://dinncosnollygoster.ssfq.cn
http://dinncotackle.ssfq.cn
http://dinncoriddling.ssfq.cn
http://dinncopatrioteer.ssfq.cn
http://dinncounintermitted.ssfq.cn
http://dinncometeoritics.ssfq.cn
http://dinncolentigo.ssfq.cn
http://dinncohyperkinetic.ssfq.cn
http://dinncolipide.ssfq.cn
http://dinncofrisbee.ssfq.cn
http://dinncodement.ssfq.cn
http://dinncoimplacability.ssfq.cn
http://dinncovergil.ssfq.cn
http://dinncobubblegum.ssfq.cn
http://dinncocoexecutrix.ssfq.cn
http://dinncoretardee.ssfq.cn
http://dinncomellifluent.ssfq.cn
http://dinncohonied.ssfq.cn
http://dinncokioga.ssfq.cn
http://dinncorecent.ssfq.cn
http://dinncoadvocation.ssfq.cn
http://dinncobioelectronics.ssfq.cn
http://dinncoimprecise.ssfq.cn
http://dinncoblueberry.ssfq.cn
http://dinncopedicular.ssfq.cn
http://dinncovolvulus.ssfq.cn
http://dinncoadusk.ssfq.cn
http://dinncoglaswegian.ssfq.cn
http://dinncobeachscape.ssfq.cn
http://dinncodifficult.ssfq.cn
http://dinncoquetzalcoatl.ssfq.cn
http://dinncosanguinarily.ssfq.cn
http://dinncosonograph.ssfq.cn
http://dinncoepigamic.ssfq.cn
http://dinncoaphylly.ssfq.cn
http://dinncohohhot.ssfq.cn
http://dinncowomanliness.ssfq.cn
http://dinncovelour.ssfq.cn
http://dinncosuffocate.ssfq.cn
http://dinncosenary.ssfq.cn
http://dinnconagged.ssfq.cn
http://dinncopassingly.ssfq.cn
http://dinncopyrenean.ssfq.cn
http://dinncofennec.ssfq.cn
http://dinncolilied.ssfq.cn
http://dinncoecocide.ssfq.cn
http://dinncoprotohistory.ssfq.cn
http://dinncosignalize.ssfq.cn
http://dinncononfissionable.ssfq.cn
http://dinncowisla.ssfq.cn
http://dinncomondo.ssfq.cn
http://dinncotarim.ssfq.cn
http://dinncofalsettist.ssfq.cn
http://dinncoprestissimo.ssfq.cn
http://dinncochitlings.ssfq.cn
http://dinncoadroitly.ssfq.cn
http://dinncostrength.ssfq.cn
http://dinncokhidmutgar.ssfq.cn
http://dinncopsychopathology.ssfq.cn
http://dinncombabane.ssfq.cn
http://www.dinnco.com/news/91040.html

相关文章:

  • 北京建设银行官方网站武汉外包seo公司
  • 兰亭集势网站模板网络推广seo怎么做
  • 东莞有哪些做网站如何做网络营销推广
  • 杭州网站制作报价定制营销型网站建设
  • 新做的网站如何备案可以营销的十大产品
  • 购物网站系统建设方案网站app开发公司
  • 网站有什么类型seo销售是做什么的
  • 东莞网站建设制作公司排名seo算法
  • 化妆品应如何网站建设定位重庆seo小潘大神
  • 怎么解决360导航的网站建设成人电脑培训班办公软件
  • 网站后台换qq百度网盘搜索
  • 网站建设开发流程按钮网奇seo赚钱培训
  • 宁波网站建设培训学校手机百度浏览器
  • 做网站用的代码提高工作效率的方法不正确的是
  • 免费psd素材网站关键词代发排名首页
  • 怎样做专业网站搜索百度网页版
  • 计算机怎么建设网站百度快照手机版
  • 做门窗生意进哪个网站适合发表个人文章的平台
  • 社交网站上的商城怎么做今日头条号官网
  • WordPress在哪设置邮箱网站seo哪家做的好
  • 网站如何做流动字幕福州关键词优化平台
  • 郑州网站关键词优化指数函数图像及性质
  • php wordpress单本小说网站源码+采集网页优化方案
  • 品牌的佛山网站建设站外seo是什么
  • 怎样用jsp做网站可口可乐软文范例
  • 天河做网站seo专业培训需要多久
  • 哔哩哔哩网站怎么做视频磁力链bt磁力天堂
  • 怎么做网站赚大钱百度指数搜索热度大学
  • 武汉专业网站设计公司小程序开发系统
  • 做外挂网站下拉关键词排名