当前位置:网站首页>[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 !
边栏推荐
- [laser principle and Application-8]: EMC design of laser circuit
- Microsoft SQL Server database language and function usage (XIII)
- [golang learning notes] simple use of flag package, command line parsing
- What if the content of software testing is too simple?
- [golang learning notes] concurrency basis
- Upgrade unity visual studio 2019 to 2022 (throw away pirated red slag)
- 接口测试
- DeFi項目的盈利邏輯 2021-04-26
- Still worrying about xshell cracking, try tabby
- 达梦数据库tools包中的工具(操作达梦数据库)
猜你喜欢

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

Tap series article 7 | easy to manage pipeline configuration

海外资深玩家的投资建议(3) 2021-05-04

El select drop-down box multi selection remote search anti display

ospf终极实验——学会ospf世纪模板例题

STM32+ESP8266+MQTT协议连接阿里云物联网平台

海外资深玩家的投资建议(2) 2021-05-03

Diabetes genetic risk testing challenge advanced

MySQL index transaction

Rails搭配OSS最佳实践
随机推荐
Raspberry pie SSH login
Introduction and project development of MVVM and mvvmlight (I)
[golang learning notes] concurrency basis
疯狂的牛市,下半场何去何从?2021-04-30
Profit logic of DFI project 2021-04-26
Can Verilog of synthetizable be integrated
Investment suggestions for overseas senior players (2) 2021-05-03
What else do entrepreneurs need besides money? Exclusive interview with Mingyue Lake venture capital institutions
zk 是如何解决脑裂问题的
About synchronizing data from computer to mobile
Memory search - DP
How ZK solves the problem of cerebral fissure
Interface test
ES6箭頭函數的使用
Excel password related
MVVM和MVVMLight简介及项目开发(一)
系列文章|云原生时代下微服务架构进阶之路 - 微服务拆分的最佳实践
思源笔记的字体比其他的编辑器(Atom,VSC,sublime)内字体渲染更细更淡
[nuxt 3] (IX) server routing
MySQL的 DDL和DML和DQL的基本语法

