当前位置:网站首页>【leetcode天梯】链表 · 022 链表中倒数第k个节点
【leetcode天梯】链表 · 022 链表中倒数第k个节点
2022-07-23 17:30:00 【kikokingzz】

题目描述:
输入一个链表,输出该链表中倒数第k个节点。为了符合大多数人的习惯,本题从1开始计数,即链表的尾节点是倒数第1个节点。
示例 1:
例如,一个链表有 5个节点,从头节点开始,它们的值依次是 1、2、3、4、5。这个链表的倒数第 2个节点是值为 4 的节点。
输入:head = [1,2,3,4,5] , k=2
输出:[4,5]示例 2:
输入:head = [1,2,3,4,5] , k=100
输出:[]示例 3:
输入:head = [] , k=1
输出:[]题目链接:
剑指 Offer 22. 链表中倒数第k个节点 - 力扣(LeetCode)
https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/链表中倒数第k个结点_牛客题霸_牛客网 (nowcoder.com)
https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&&tqId=11167&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
这边牛客网的检验事例更多,推荐各位使用牛客暴刷!
解题思路:
常规法1:早晚指针
这是一种非常经典的做法,是快慢指针做法的变形操作。我们设置两个指针,其中指针late比指针early早走k步,然后让指针early与指针late共同向后遍历,当指针late指向NULL时,指针early指向的结点恰好就是倒数第k个结点。
struct ListNode* getKthFromEnd(struct ListNode* head, int k){ struct ListNode* late = head; struct ListNode* early = head; while(k--) { if(late!=NULL) //当late不为空时,late一直向后走 late=late->next; else return NULL;//当未走满k步late就已经走到NULL时,直接返回NULL,得不到倒数第k个结点 } while(late) //late指针与early指针一直走到late指针指向空时才停止 { early=early->next; //early向后走一步 late=late->next; //late向后走走一步 } return early; }
总结一下:这是非常典型的快慢指针问题,相信各位看一遍就能记住这种方法,非常经典好用!
边栏推荐
- mBio | 海洋所孙超岷组在深海原位验证了微生物介导的单质硫形成新通路
- Learn and understand Architecture Design from business development
- [paper reading] gettext: trajectory flow map enhanced transformer for next POI recommendation
- ? The problem of front desk parameter transmission needs to be confirmed
- 国外芯片,为什么有中文资料和网页?
- Error reporting caused by the use of responsebodyadvice interface and its solution
- Tcl 语言之Synopsys Tcl篇(3)(数字IC)
- Gradle [graphic installation and use demonstration]
- FPGA flash reading and writing based on SPI
- .net core implements background tasks (scheduled tasks) longbow Tasks component (III)
猜你喜欢

国外芯片,为什么有中文资料和网页?

去中心化存储面临的挑战

ES6其他语法及扩展语法总结

What is stack and the difference between stacks

How can win11 add 3D effects to pictures? Win11 method of adding picture 3D effect

Building virtual private network based on softther

How can zero foundation self-study software testing? Ten year test Laoniao's strongest software test learning Roadmap

使用 frp 实现内网穿透

Detailed explanation: tmp1750 chip three channel linear LED driver

Digital security giant entrust revealed that it was attacked by blackmail software gangs in June
随机推荐
有向图之求两点之间所有路径
FPGA flash reading and writing based on SPI
C语言小项目 -- 通讯录(静态版+动态版+文件版)
FPGA基于spi的flash读写
[2022] [paper notes] terahertz quantum well——
JUC concurrent programming [detailed explanation and demonstration]
Challenges of decentralized storage
Perl语言简述
LeetCode刷题:回文数
【C语言】程序环境和预处理
[2020] [paper notes] optically controlled spectral ratio adjustable y based on two-dimensional photonic crystal——
Fragment
mysqldump导出内容如何不带 comment ?
MySQL [knowing and mastering one article is enough]
小鱼送激光雷达啦 | 恰饭即抽奖第一期
[paper reading] gettext: trajectory flow map enhanced transformer for next POI recommendation
Leetcode daily question 2022/7/18-2022/4/24
Detailed explanation: tmp1750 chip three channel linear LED driver
LeetCode每日一题(1514. Path with Maximum Probability)
UPC 2022 summer personal training game 12 (number of combinations b)

