当前位置:网站首页>[leetcode ladder] the penultimate node in the 022 linked list
[leetcode ladder] the penultimate node in the 022 linked list
2022-07-23 22:53:00 【kikokingzz】

Title Description :
Enter a linked list , Output the last number in the list k Nodes . In order to conform to the habits of most people , From 1 Start counting , That is, the tail node of the list is the last 1 Nodes .
Example 1:
for example , A list has 5 Nodes , Start from the beginning , Their values, in turn, are 1、2、3、4、5. The last of the list 2 Each node has a value of 4 The node of .
Input :head = [1,2,3,4,5] , k=2
Output :[4,5]Example 2:
Input :head = [1,2,3,4,5] , k=100
Output :[]Example 3:
Input :head = [] , k=1
Output :[]Topic link :
The finger of the sword Offer 22. Last in the list k Nodes - Power button (LeetCode)
https://leetcode.cn/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/ Last in the list k Nodes _ Niuke Tiba _ Cattle from (nowcoder.com)
https://www.nowcoder.com/practice/529d3ae5a407492994ad2a246518148a?tpId=13&&tqId=11167&rp=2&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
There are more test cases of Niuke here , I recommend you to use Niuke brush !
Their thinking :
Conventional method 1: Sooner or later
This is a very classic approach , It is the deformation operation of fast and slow pointer . We set two pointers , Where the pointer late Than the pointer early Go early k Step , Then let the pointer early With the pointer late Common backward traversal , When the pointer late Point to NULL when , The pointer early The node pointed to happens to be the penultimate k Nodes .
struct ListNode* getKthFromEnd(struct ListNode* head, int k){ struct ListNode* late = head; struct ListNode* early = head; while(k--) { if(late!=NULL) // When late Isn't empty ,late Go straight back late=late->next; else return NULL;// When not full k Step late Have already walked to NULL when , Go straight back to NULL, Can't get the penultimate k Nodes } while(late) //late The pointer and early The pointer goes all the way to late It stops when the pointer points to null { early=early->next; //early Take a step back late=late->next; //late Take a step backwards } return early; }
To sum up : This is a very typical speed pointer problem , I believe you can remember this method by looking at it once , Very classic and easy to use !
边栏推荐
- torchvision.datasets.ImageFolder前的数据整理及使用方法
- 年化收益率6%的理财产品
- 121. The best time to buy and sell stocks
- 糖尿病遗传风险检测挑战赛Baseline
- VIM common shortcut keys
- The font of Siyuan notes is thinner and lighter than that in other editors (atom, VSC, sublime)
- Remember an experience of being cheated by the Internet
- D1-H 开发板——哪吒 开发入门
- 礪夏行動|源啟數字化:既有模式,還是開源創新?
- TAP 系列文章4 | 基于 Backstage 的 TAP 开发者门户
猜你喜欢

TAP 系列文章7 | 易于管理的流水线配置

TAP 系列文章8 | TAP 学习中心——通过动手教程来学习

Mongodb - Introduction to the usage of logical operators not, and, or, nor in query statements

As a developer, you have to know the three performance testing tools JMeter, API and jmh user guide
思源笔记的字体比其他的编辑器(Atom,VSC,sublime)内字体渲染更细更淡

Can Verilog of synthetizable be integrated

SOLIDWORK learning notes: Sketch geometric relationships and editing

Remember an experience of being cheated by the Internet

Microsoft SQL Server数据库语言及功能使用(十三)

What else do entrepreneurs need besides money? Exclusive interview with Mingyue Lake venture capital institutions
随机推荐
Internet协议栈 TCP/IP模型 物理层、链路层、网络层、传输层、应用层的作用
Array -- 209. Subarray with the smallest length
Array - 59. Spiral matrix II
DHCP: prevent rogue DHCP server in the network
Series of articles | the way to advance the microservice architecture in the cloud native era - best practices of microservice splitting
Utilisation des fonctions fléchées es6
小说里的编程 【连载之十六】元宇宙里月亮弯弯
Linked list - 203. remove linked list elements
Microsoft SQL Server database language and function usage (XIII)
fl studio 20.9更新中文版宿主DAW数字音频工作站
dried food! Implicit sparse regularization effect in neural networks
FL Studio 20.9 update Chinese version host Daw digital audio workstation
TAP 系列文章6 | TAP的应用模型
Getting started database days3
思源笔记的字体比其他的编辑器(Atom,VSC,sublime)内字体渲染更细更淡
Wangxuegang video coding -- mediacodec coding and decoding
Is Ping An Securities' low commission account opening link safe? How to handle low commission
unity visual studio2019升级到2022版本(扔掉盗版红渣)
EasyNVR平台如何关闭匿名登录?
D2admin framework is basically used

