当前位置:网站首页>The sword refers to the offer question 22 - the Kth node from the bottom in the linked list
The sword refers to the offer question 22 - the Kth node from the bottom in the linked list
2022-08-03 22:24:00 【Avi's Blog Diary】
假设有n个节点,Then calculate the penultimatek个节点的值,要用双指针
思想:
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,BThe pointer points to the end element
8
So the subscript distance between the two pointers is always differentk-1
=2个(This is easy to derive,因为倒数第k个
和末尾元素下标
The distance between is necessary减去第koccupied by an element1个位置
,减1是容易推导的),So when you analyze this relationship,你把A BThe pointers are respectively translated to point to the first element of the linked list,第1elements go backwardsk-1The position of the element of the step
,Then the two pointers are separated again同步
依次遍历next指针,Traversing to the end is our initial situation,此时AThe pointer happens to point to the first of the linked listk个的位置
因此,要保证B指针和APointers always differk-1的距离,就要先把Bpointer movesk-1次,Then double pointer synchronization is facilitated untilB指针的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 A pointer to the head of a linked list */
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 A pointer to the head of a linked list */
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;
}
边栏推荐
猜你喜欢
CAS: 1192802-98-4 _uv cracking of biotin - PEG2 - azide
中国企业构建边缘计算解决方案的最佳实践
IO thread process -> thread synchronization mutual exclusion mechanism -> day6
Boss: There are too many systems in the company, can you realize account interoperability?
YOLO之父宣布退出CV界,坦言无法忽视自己工作带来的负面影响
CAS:153162-70-0_N-BOC-6-Biotinamidohexylamine
重发布实验报告
Go开发工具GoLand V2022.2 来了——Go 工作区重大升级
目标检测技术研究现状及发展趋势
趣链的产品构架
随机推荐
数据一致性:双删为什么要延时?
授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节试读版
关于Yii2批量更新的操作
start with connect by implements recursive query
DO280管理和监控OpenShift平台--资源限制
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
Shell编程的条件语句
UVa 437 - The Tower of Babylon (White Book)
Kubernetes入门到精通-Operator 模式
【day6】类与对象、封装、构造方法
Testng监听器
Summary bug 】 【 Elipse garbled solution project code in Chinese!
113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
21天打卡挑战学习MySQL—Day第一周 第一篇
云计算国内外发展现状
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
Boss: There are too many systems in the company, can you realize account interoperability?
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!
Bytebase database schema change management tool