当前位置:网站首页>Linked list - the penultimate K nodes
Linked list - the penultimate K nodes
2022-07-26 18:21:00 【Morita Rinko】
subject :
Enter a length of n The linked list of , Set the value of the element in the linked list to ai , Returns the penultimate... In the linked list k Nodes .
If the length of the list is less than k, Please return a length of 0 The linked list of .
For example, the input {1,2,3,4,5},2 when , The corresponding linked list structure is shown in the figure below :
The blue part is the last part of the linked list 2 Nodes , So return to the penultimate 2 Nodes ( That is, the node value is 4 The node of ) that will do , The system will print all the following nodes to compare .
Their thinking :
Set two pointers ,flag and cur,flag When you reach the end of the linked list ,flag And cur Distance identification between k value .
Solution code :
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
*
*
* @param pHead ListNode class
* @param k int integer
* @return ListNode class
*/
ListNode* FindKthToTail(ListNode* pHead, int k) {
// write code here
ListNode* flag=pHead;
ListNode* cur=pHead;
for(int i=0;i<k;i++){
if(!flag){
return nullptr;// When flag The value is null When , Express k Value is greater than the length of the linked list
}
flag=flag->next;
}
while(flag){
cur=cur->next;
flag=flag->next;
}
return cur;
}
};
边栏推荐
- 7、 Common commands of ROS (II): rosservice, rossrv, rosparam
- Leetcode 0137. number II that appears only once
- Linux安装mysql8.0.29详细教程
- 剑指offer 连续子数组的最大和(二)
- 分布式链路追踪Jaeger在Golang中的使用
- 同步时现实密码不匹配
- AI zhetianchuan ml integrated learning
- LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)
- How about the employment prospects of Russian translation? How to do a good job of Russian translation
- 10、 Implementation of parameter modification of parameter server
猜你喜欢

线性回归——以一道等差数列的题为例

Apartment rental system based on JSP

The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
![[day3] reconstruction of roads](/img/52/cc8b81bccbf4aa02ec82fedfb49d19.png)
[day3] reconstruction of roads

LeetCode50天刷题计划(Day 4—— 最长回文子串 14.00-16:20)

ssm练习第三天_分页助手_安全框架

Vector CANoe Menu Plugin拓展入门

2022年的PMP考试大纲是什么?

俄语翻译的就业前景怎样 如何做好俄语翻译工作

Oracle第二天(视图、索引、plsql、游标、存储过程和存储函数、触发器、jdbc访问存储过程和存储函数)
随机推荐
9、 Alternative implementation of client for service communication
Leetcode 0139. word splitting
Three ways of de duplication in SQL
PMP考试详解,新考纲有什么变化?
[Digital IC] understand Axi Lite protocol in simple terms
Data warehouse: fact table of detailed dimensional modeling
Detailed explanation of openwrt's feeds.conf.default
OpenWrt之feeds.conf.default详解
Continue to work hard on your skills, and the more you learn, the more you will learn
[training Day2] torchbearer
Oracle第一天(开发常用的知识点再回顾整理下)
URL jump vulnerability
The second set of 2020 American Asian individual match
【一知半解】线程池
钉钉第三方服务商应用ISV应用开发及上架教程
[static code quality analysis tool] Shanghai daoning brings you sonarource/sonarqube download, trial and tutorial
It is said that the salary of Alibaba P7 is really fragrant
Zhaoqi science and technology innovation overseas high-level talent introduction platform, entrepreneurship event Roadshow
How to become a better data scientist by learning to ask questions
8.2 some algebraic knowledge (groups, cyclic groups and subgroups)