当前位置:网站首页>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 element8
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;
}
边栏推荐
- 【MySQL进阶】数据库与表的创建和管理
- 云计算国内外发展现状
- mysql如何将表结构导出到excel
- UVa 10003 - Cutting Sticks (White Book, Interval DP)
- Embedded Systems: GPIO
- Boss: There are too many systems in the company, can you realize account interoperability?
- 生成器版和查看器版有什么区别?
- 藏宝计划TreasureProject(TPC)系统模式开发技术原理
- 【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
- 【bug】汇总Elipse项目中代码中文乱码解决方法!
猜你喜欢

静态文件快速建站

嵌入式开发:嵌入式基础——代码和数据空间揭秘

override学习(父类和子类)

【历史上的今天】8 月 3 日:微软研究院的创始人诞生;陌陌正式上线;苹果发布 Newton OS

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

What is Adobe?
![[MySQL Advanced] Creation and Management of Databases and Tables](/img/31/2015122e409148b3679b09a03db869.png)
[MySQL Advanced] Creation and Management of Databases and Tables

113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节

2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis

LabVIEW代码生成错误 61056
随机推荐
【bug】汇总Elipse项目中代码中文乱码解决方法!
PowerMockup 4.3.4::::Crack
一文带你了解软件测试是干什么的?薪资高不高?0基础怎么学?
【day1】
Data_web(八)mysql增量同步到mongodb
Embedded Systems: Clocks
如何设计 DAO 的 PoW 评判标准 并平衡不可能三角
CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
深度学习和机器学习有什么区别?
【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
JPA Native Query(本地查询)及查询结果转换
YOLO之父宣布退出CV界,坦言无法忽视自己工作带来的负面影响
384. Shuffle an Array
嵌入式开发:嵌入式基础——代码和数据空间揭秘
九种方式,教你读取 resources 目录下的文件路径
Bytebase数据库 Schema 变更管理工具
CAS:908007-17-0_Biotin-azide_Biotin azide
【云原生实用技巧】使用 skopeo 批量同步 helm chart 依赖镜像
云平台建设解决方案
斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!