当前位置:网站首页>[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 !
边栏推荐
- Life always needs a little passion
- Raspberry pie SSH login
- Tap series article 5 | cloud native build service
- 121. The best time to buy and sell stocks
- Mongodb - Introduction to the use of $exists and the combination of $ne, $nin, $nor, $not in query statements
- Drools (1): introduction to drools
- The ultimate experiment of OSPF -- learn the example of OSPF century template
- Getting started database days3
- Interface test
- STM32 MCU uses ADC function to drive finger heartbeat detection module
猜你喜欢

Can Verilog of synthetizable be integrated

Explain NAT technology in detail

synthesizable之Verilog可不可综合

TAP 系列文章6 | TAP的应用模型

使用itextpdf提取PDF文件中的任意页码

Microsoft SQL Server database language and function usage (XIII)

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

openEuler 资源利用率提升之道 01:概论

【Unity3D日常BUG】Unity3D解决“找不到类型或命名空间名称“XXX”(您是否缺少using指令或程序集引用?)”等问题

Mongodb - Introduction to the use of $exists and the combination of $ne, $nin, $nor, $not in query statements
随机推荐
Preparation for raspberry pie 3B serial port login
DeFi项目的盈利逻辑 2021-04-26
QT set cache and compile output path
Build your own target detection environment, model configuration, data configuration mmdetection
Diabetes genetic risk testing challenge advanced
STM32单片机使用ADC功能驱动手指检测心跳模块
小说里的编程 【连载之十六】元宇宙里月亮弯弯
D1-h development board - Introduction to Nezha development
Microsoft SQL Server数据库语言及功能使用(十三)
Matlab小波工具箱导入信号出错(doesn‘t contain one dimensional Singal)
ES6 use of arrow function
系列文章|云原生时代下微服务架构进阶之路 - 微服务拆分的最佳实践
Tap series article 7 | easy to manage pipeline configuration
El select drop-down box multi selection remote search anti display
[Matplotlib drawing]
小说里的编程 【连载之十七】元宇宙里月亮弯弯
13. Roman to integer
Data sorting and usage before torchvision.datasets.imagefolder
Mongodb - Introduction to the usage of logical operators not, and, or, nor in query statements
[C language] address book (static version)

