当前位置:网站首页>剑指offer第22题-链表中倒数第K个节点
剑指offer第22题-链表中倒数第K个节点
2022-08-03 22:20:00 【阿维的博客日记】
假设有n个节点,则计算倒数第k个节点的值,要用双指针
思想:
n个节点,倒数第k个节点。可以画一个图,有9个元素,分别有
a[0]=0
a[1]=1
a[2]=2
a[3]=3
a[4]=4
a[5]=5
a[6]=6
a[7]=7
a[8]=8
然后假设k=3,A指针指向倒数第k个元素6,B指针指向末尾元素
8
因此两个指针之间的下标距离始终相差k-1
=2个(这个容易推导,因为倒数第k个
和末尾元素下标
之间距离就是要减去第k个元素所占的1个位置
,减1是容易推导的),因此当你分析出这个关系,你把A B指针分别平移指向链表第一个元素,第1个元素往后走k-1步的元素的位置
,然后两个指针再次分别同步
依次遍历next指针,遍历到最后就是我们最初的情况,此时A指针恰好指向链表第k个的位置
因此,要保证B指针和A指针始终相差k-1的距离,就要先把B指针先后移k-1次,然后双指针同步便利直到B指针的next为nullptr
最后代码如下,problem22.h是声明,problem22.cpp是函数的实现,最后main.cpp是main函数problem22.h
#ifndef PROBLEM_PROBLEM22_H
#define PROBLEM_PROBLEM22_H
#include <iostream>
struct node {
int value;
struct node *next;
node(int value){
this->value=value;
this->next= nullptr;
}
};
typedef struct node *pnode;
typedef unsigned int uint;
pnode FindKthToTail(pnode pListHead, uint k) ;
void add(pnode& head,int value);
void print(pnode h);
/** * * * @return 一个链表的头指针 */
pnode init();
#endif //PROBLEM_PROBLEM22_H
problem22.cpp
#include <iostream>
#include "problem22.h"
typedef node *pnode;
typedef unsigned int uint;
pnode FindKthToTail(pnode pListHead, uint k) {
pnode pAhead = pListHead;
pnode pBehind = nullptr;
for (int i = 0; i < k - 1; ++i) {
pAhead = pAhead->next;
}
pBehind = pListHead;
while (pAhead->next != nullptr) {
pAhead = pAhead->next;
pBehind = pBehind->next;
}
return pBehind;
}
void add(pnode& head,int value){
if(head== nullptr){
head=new node(value);
return;
}
pnode ptr=head;
while (ptr->next!= nullptr){
ptr=ptr->next;
}
pnode new_node=new node(value);
ptr->next=new_node;
return;
}
void print(pnode h){
pnode ptr=h;
while (ptr){
printf("%d " ,ptr->value);
ptr=ptr->next;
}
}
/** * * * @return 一个链表的头指针 */
pnode init(){
pnode head=new node(1);
add(head,2);
add(head,3);
add(head,4);
add(head,5);
add(head,6);
add(head,7);
add(head,8);
add(head,9);
// add(head,10);
// print(head);
return head;
}
main.cpp
#include <iostream>
#include "problems/problem22.h"
using namespace std;
int main() {
std::cout << "Hello, World!" << std::endl;
pnode hA = init();
pnode node = FindKthToTail(hA,3);
printf("%d",node->value);
return 0;
}
边栏推荐
- HCIP第十四天
- 2019年10月SQL注入的两倍
- IO thread process -> thread synchronization mutual exclusion mechanism -> day6
- 老板:公司系统太多,能不能实现账号互通?
- 东西向和南北向通信的统一
- 488. Zuma Game
- pikachu Over permission 越权
- 113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
- Basic Concepts of Graphs
- 2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
猜你喜欢
for循环练习题
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
CAS:1620523-64-9_Azide-SS-biotin_生物素-二硫-叠氮
Flutter 桌面探索 | 自定义可拖拽导航栏
pikachu Over permission 越权
Summary bug 】 【 Elipse garbled solution project code in Chinese!
Pay from 0 to 1
【day6】类与对象、封装、构造方法
FVCOM三维水动力、水交换、溢油物质扩散及输运数值模拟丨FVCOM模型流域、海洋水环境数值模拟方法
用于流动质押和收益生成的 Web3 基础设施
随机推荐
On the Qixi Festival of 2022, I will offer 7 exquisite confession codes, and at the same time teach you to quickly change the source code for your own use
386. Lexicographical Numbers
Golang第一章:入门
472. Concatenated Words
Internet user account information management regulations come into effect today: must crack down on account trading and gray products
21天打卡挑战学习MySQL——《MySQL工具的使用》第一周 第二篇
L2-029 特立独行的幸福
CAS:122567-66-2_DSPE-生物素_DSPE-Biotin
如何基于WPF写一款数据库文档管理工具(二)
嵌入式系统:GPIO
2022-08-03 Oracle executes slow SQL-Q17 comparison
IO线程进程->线程同步互斥机制->day6
【刷题篇】二叉树的右视图
Flutter 桌面探索 | 自定义可拖拽导航栏
亿流量大考(2):开发一套高容错分布式系统
win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
Cisco ike2 IPSec配置
【day6】类与对象、封装、构造方法
『百日百题 · 基础篇』备战面试,坚持刷题 第四话——循环语句!
IO thread process -> thread synchronization mutual exclusion mechanism -> day6