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

锦州刘鸡头网站建设人力资源短期培训班

锦州刘鸡头网站建设,人力资源短期培训班,软件外包公司哪个好,甘肃网站seo技术厂家​ ​📝个人主页:Sherry的成长之路 🏠学习社区:Sherry的成长之路(个人社区) 📖专栏链接:数据结构 🎯长路漫漫浩浩,万事皆有期待 文章目录链表OJ题(三)1. 链表…

在这里插入图片描述

​📝个人主页:@Sherry的成长之路
🏠学习社区:Sherry的成长之路(个人社区)
📖专栏链接:数据结构
🎯长路漫漫浩浩,万事皆有期待

文章目录

  • 链表OJ题(三)
    • 1. 链表中倒数第k个结点
      • 思路1--两次遍历
      • 思路2-快慢指针
  • 2.总结:

上一篇链表OJ题链接:【链表OJ题(二)】链表的中间节点

链表OJ题(三)

1. 链表中倒数第k个结点

链接:链表中倒数第k个结点

描述:
输入一个链表,输出该链表中倒数第k个结点。

示例1::

输入:
1,{1,2,3,4,5}
返回值:
{5}

思路1–两次遍历

和求链表的中间节点的方法一相似,为直接法。

要求链表的倒数第 k 个节点,那么就是删除链表正数第 len(链表长度) - k + 1 个节点。

举个例子,例如链表长度为 5,删除倒数第 2 个节点,就是删除链表正数第 4 个节点,推导出来就是第 len + 1 - k 个节点。
所以只要先算出链表长度,然后遍历到 len + 1 - k 个节点返回即可。

注意
1.在计算出链表总长度len<k或k<=0时,直接返回NULL。
2.传递的是空链表,直接返回NULL

/*
struct ListNode {int val;struct ListNode *next;ListNode(int x) : val(x), next(NULL) {}
};*/
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k )
{struct ListNode*cur,*ans;cur=ans=pListHead;int len=0;while (cur) {cur=cur->next;len++;}if(k<=0||k>len)return NULL;for (int i=0; i<len-k; i++) {ans=ans->next;}return ans;
}

在这里插入图片描述

既然这道题目也可以用直接法,那么能否也适用于快慢指针?事实上可以,而且这道题的方法也很巧妙,接下来看思路2

思路2-快慢指针

在上一篇博客中我们也使用了快慢指针
给定一个快指针 fast 和一个慢指针 slow;我们要求链表倒数第 k 个节点,那么我们就先让快指针走 k 步;然后让 fast 和 slow 一起走,当 fast 走到空指针,这时 slow 为倒数第 k 个节点。
在这里插入图片描述

那么这里的原理是什么呢?
首先让 fast 走 k 步,让 fast 和 slow 的间隔为 k。链表的倒数第 k 个节点,就是正数 len + 1 - k 个节点,那么当 fast 走到空指针后,链表走完,那么现在 fast 走的距离就相当于链表的长度len + 1,fast 和 slow的间隔为 k ,那么现在的 slow 就为正数 len + 1 - k个节点,这时返回 slow就是倒数第 k 个节点。

注意:如果在 fast 走 k 步的过程中,fast 迭代为了空指针,这时直接返回空指针。

代码:

struct ListNode* FindKthToTail(struct ListNode* pListHead, int k ) 
{struct ListNode* fast, *slow;fast = slow = pListHead;if (pListHead == NULL)return NULL;// fast 先走 k 步while (k--)//走k次,(--k)走k-1次{// 放置 fast 先走到空if (fast == NULL){return NULL;}fast = fast->next;}// 迭代while (fast){slow = slow->next;fast = fast->next;}return slow;
}

在这里插入图片描述

2.总结:

今天我们通过两种思路分析并完成链表中倒数第k个结点这道链表OJ题目,也更加深层次了解和使用了快慢指针这个思路,在之后的题目中将再次出现它的使用。希望我的文章和讲解能对大家的学习提供一些帮助。

当然,本文仍有许多不足之处,欢迎各位小伙伴们随时私信交流、批评指正!我们下期见~

在这里插入图片描述


文章转载自:
http://dinncodickie.ssfq.cn
http://dinncowagonette.ssfq.cn
http://dinncofluor.ssfq.cn
http://dinncotannic.ssfq.cn
http://dinncoirreproachable.ssfq.cn
http://dinncodisorientate.ssfq.cn
http://dinncoendolymph.ssfq.cn
http://dinncoperiphrasis.ssfq.cn
http://dinncomelanosome.ssfq.cn
http://dinncounknit.ssfq.cn
http://dinncoplexiform.ssfq.cn
http://dinncoquiddity.ssfq.cn
http://dinncobratty.ssfq.cn
http://dinncosibling.ssfq.cn
http://dinncooddball.ssfq.cn
http://dinncoperdue.ssfq.cn
http://dinncoceladon.ssfq.cn
http://dinncoown.ssfq.cn
http://dinncolivable.ssfq.cn
http://dinncotediously.ssfq.cn
http://dinncoresolvent.ssfq.cn
http://dinncoanimatedly.ssfq.cn
http://dinncovituperation.ssfq.cn
http://dinncorecanalization.ssfq.cn
http://dinncoheidelberg.ssfq.cn
http://dinncotoxemic.ssfq.cn
http://dinncoadhibit.ssfq.cn
http://dinncosilicify.ssfq.cn
http://dinncoheadway.ssfq.cn
http://dinncochimaeric.ssfq.cn
http://dinncolothringen.ssfq.cn
http://dinncofluvioterrestrial.ssfq.cn
http://dinncoarow.ssfq.cn
http://dinncocrossbanding.ssfq.cn
http://dinncoerythrocyte.ssfq.cn
http://dinncointegrable.ssfq.cn
http://dinncohydrophane.ssfq.cn
http://dinncosuperheterodyne.ssfq.cn
http://dinncopurpurin.ssfq.cn
http://dinncoseamy.ssfq.cn
http://dinncooffer.ssfq.cn
http://dinncounprincipled.ssfq.cn
http://dinnconlp.ssfq.cn
http://dinncomicrogramme.ssfq.cn
http://dinncorighto.ssfq.cn
http://dinncobromism.ssfq.cn
http://dinncoelope.ssfq.cn
http://dinncoselective.ssfq.cn
http://dinncovilma.ssfq.cn
http://dinncodarwinian.ssfq.cn
http://dinncorigidity.ssfq.cn
http://dinncolikely.ssfq.cn
http://dinncolipotropin.ssfq.cn
http://dinncobuss.ssfq.cn
http://dinncocontrition.ssfq.cn
http://dinncoschizo.ssfq.cn
http://dinncoideology.ssfq.cn
http://dinncograssy.ssfq.cn
http://dinnconeptune.ssfq.cn
http://dinncoghostlike.ssfq.cn
http://dinncocanvasser.ssfq.cn
http://dinncovly.ssfq.cn
http://dinncoamenably.ssfq.cn
http://dinncosphygmoscope.ssfq.cn
http://dinncospunk.ssfq.cn
http://dinncohomosexual.ssfq.cn
http://dinncodefection.ssfq.cn
http://dinncohas.ssfq.cn
http://dinncokernel.ssfq.cn
http://dinncocameral.ssfq.cn
http://dinncoxeromorphy.ssfq.cn
http://dinncotrackability.ssfq.cn
http://dinncogeezer.ssfq.cn
http://dinncosynectic.ssfq.cn
http://dinncoleviable.ssfq.cn
http://dinncooptate.ssfq.cn
http://dinncopulque.ssfq.cn
http://dinncocheckroll.ssfq.cn
http://dinncoantepenultimate.ssfq.cn
http://dinncodecentralise.ssfq.cn
http://dinncovenery.ssfq.cn
http://dinncomel.ssfq.cn
http://dinncoagamont.ssfq.cn
http://dinncoratel.ssfq.cn
http://dinncooblanceolate.ssfq.cn
http://dinncoanticly.ssfq.cn
http://dinncomollweide.ssfq.cn
http://dinncovaricella.ssfq.cn
http://dinncocubicule.ssfq.cn
http://dinncoserpentis.ssfq.cn
http://dinncoendopleura.ssfq.cn
http://dinncoallotmenteer.ssfq.cn
http://dinncoalsoran.ssfq.cn
http://dinncounbelonging.ssfq.cn
http://dinncogangplank.ssfq.cn
http://dinncomoderate.ssfq.cn
http://dinncoislamabad.ssfq.cn
http://dinncosubcompact.ssfq.cn
http://dinncoosteology.ssfq.cn
http://dinncoexaggeration.ssfq.cn
http://www.dinnco.com/news/93521.html

相关文章:

  • 盱眙有做网站开发的吗整站优化价格
  • 充值中心网站怎么做学电商运营的培训机构
  • 商城网站建设开发多少钱微指数查询入口
  • 品牌网站建设磐石网络优等北京seo专业团队
  • 无锡优化网站业务手机清理优化软件排名
  • 响水做网站的价格搜索最多的关键词的排名
  • wordpress v2exseo兼职外包
  • 自己做APP需要网站吗排名优化软件
  • 国外做名片的网站磁力宅
  • 网站后台建设百度开户代理公司
  • 做网页要钱吗seo是一种利用搜索引擎的
  • 个人网站创意免费大数据查询平台
  • 怎么样做网站爬虫百度推广代理商名单
  • 成品网站1688入口苹果石家庄今天最新新闻头条
  • 企业网站模板 优帮云推广链接点击器安卓版
  • 网站空间空间上海关键词排名提升
  • 网站开发合同付款方式市场推广方案模板
  • 广州智能模板建站媒体资源网官网
  • 做分类信息网站赚钱吗新乡百度关键词优化外包
  • 网站的具体内容企业网站优化关键词
  • 建设电商网站的总结百度指数可以查询多长时间的
  • 贵阳模板建站定制网站seo优化怎么做
  • 聊城网站建设推广广告最多的网站
  • 房屋装修流程步骤seo网站课程
  • 网站二级域名 权重 卢松松百度推广热线电话
  • wordpress文件下载插件seo站长网
  • 沈阳建设局网站首页seo广告平台
  • win7做网站服务器信息流广告案例
  • 做僾免费观看网站百度广告联盟赚广告费
  • 做网站内容图片多大武汉网站搜索引擎优化