当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

嵌入式系统:时钟

目标检测技术研究现状及发展趋势

encapsulation, package, access modifier, static variable

Boss: There are too many systems in the company, can you realize account interoperability?

重发布实验报告

E-commerce data warehouse ODS layer-----log data loading

for循环练习题

网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)

【bug】汇总Elipse项目中代码中文乱码解决方法!

CAS:1797415-74-7_TAMRA-Azide-PEG-Biotin
随机推荐
Zilliz 2023 秋季校园招聘正式启动!
.NET6之MiniAPI(十四):跨域CORS(上)
HCIP第十五天
L2-029 特立独行的幸福
Canvas App中点击图标生成PDF并保存到Dataverse中
CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
亿流量大考(2):开发一套高容错分布式系统
DO280管理和监控OpenShift平台--资源限制
目标检测技术研究现状及发展趋势
Cisco ike2 IPSec configuration
[b01lers2020]Life on Mars
CAS: 1192802-98-4 _uv cracking of biotin - PEG2 - azide
Nine ways to teach you to read the file path in the resources directory
超级实用网站+公众号合集
mysql如何将表结构导出到excel
golang写的存储引擎,基于b+树,mmap
UVa 1025 - A Spy in the Metro (White Book)
[N1CTF 2018]eating_cms
Research status of target detection at home and abroad
Data_web(九)mongodb增量同步到mongodb