当前位置:网站首页>链表-倒数最后k个结点
链表-倒数最后k个结点
2022-07-26 17:31:00 【早田凛凛子】
题目:
输入一个长度为 n 的链表,设链表中的元素的值为 ai ,返回该链表中倒数第k个节点。
如果该链表长度小于k,请返回一个长度为 0 的链表。
例如输入{1,2,3,4,5},2时,对应的链表结构如下图所示:
其中蓝色部分为该链表的最后2个结点,所以返回倒数第2个结点(也即结点值为4的结点)即可,系统会打印后面所有的节点来比较。
解题思路:
设置两个指针,flag和cur,flag到达链表尾部的时候,flag与cur之间的距离标识k值。
解题代码:
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) : val(x), next(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pHead ListNode类
* @param k int整型
* @return ListNode类
*/
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;//当flag值为null的时候,表示k值大于链表的长度
}
flag=flag->next;
}
while(flag){
cur=cur->next;
flag=flag->next;
}
return cur;
}
};
边栏推荐
- 4、 Service communication principle, code implementation
- SQL判断某列中是否包含中文字符、英文字符、纯数字,数据截取
- 3、 Topic communication: create your own information format
- The Agile Manifesto has four values and twelve principles
- Spark unified memory partition
- 2、 Topic communication principle, code implementation
- [Digital IC] understand Axi Lite protocol in simple terms
- 【集训Day2】Sculpture
- Interview with celebrities | open source is a double-edged sword for security -- Wei Jianfan, author of the Chinese translation of cathedral and market
- 1、 C language program structure, compilation and operation, data type related
猜你喜欢
![[metauniverse OMI theory] analyze Web3 risk challenges and build Web3 ecological security](/img/d1/4a424d810f7a6aaccae80b4fe232c0.png)
[metauniverse OMI theory] analyze Web3 risk challenges and build Web3 ecological security

推荐效果不如意,不如试试飞桨图学习

Redisdesktopmanager removes the upgrade prompt

How to assemble a registry?

Come on developer! Not only for the 200000 bonus, try the best "building blocks" for a brainstorming!

During the oppo interview, 16 questions were thrown over. I was stupid

Week 17 free intrusion pointer exercise - output maximum

Relative path and absolute path
![[training day3] delete](/img/7b/40bfb7710427696a27796428d849ba.png)
[training day3] delete

2022年的PMP考试大纲是什么?
随机推荐
Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
百度飞桨EasyDL X 韦士肯:看轴承质检如何装上“AI之眼”
What is the PMP exam outline in 2022?
The chess robot broke the finger of a 7-year-old boy because "the chess player violated safety rules"?
深度学习实验:Softmax实现手写数字识别
长征证券开户安全吗?
AI zhetianchuan ml unsupervised learning
【集训Day1】 Dwarves line up
Is it safe for me to open the securities account of CITIC and find the channel manager?
LeetCode 0139. 单词拆分
Mondriaans's dream (state compression DP)
8.2 一些代数知识(群、循环群和子群)
[training Day1] Dwaves line up
Click hijacking attack
Spark数据格式UnsafeRow
PMP考试详解,新考纲有什么变化?
OpenWrt之feeds.conf.default详解
CentOS installs docker and MySQL and redis environments
Win10 wireless connection cannot input password characters, and it will be stuck as soon as it is input
Deep learning experiment: softmax realizes handwritten digit recognition