当前位置:网站首页>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;
}
边栏推荐
- start with connect by implements recursive query
- 466. Count The Repetitions
- 2019年10月SQL注入的两倍
- 113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
- UVa 10003 - Cutting Sticks (White Book, Interval DP)
- Pay from 0 to 1
- 【MySQL进阶】数据库与表的创建和管理
- UVa 10003 - Cutting Sticks(白书,区间DP)
- 2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用
- CAS: 1192802-98-4 _uv cracking of biotin - PEG2 - azide
猜你喜欢
随机推荐
【bug】汇总Elipse项目中代码中文乱码解决方法!
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
Basic Concepts of Graphs
Data_web(九)mongodb增量同步到mongodb
目标检测技术研究现状及发展趋势
466. Count The Repetitions
CAS:1260586-88-6_Biotin-C5-Azide_Biotin-C5-Azide
FinClip最易用的智能电视小程序
【day6】类与对象、封装、构造方法
优化查询(工作中)
noip初赛
数据一致性:双删为什么要延时?
Kubernetes入门到精通-Operator 模式
互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产
376. Wiggle Subsequence
LabVIEW代码生成错误 61056
Flink--Join以及Flink函数
投资性大于游戏性 NFT游戏到底是不是门好生意
Cisco ike2 IPSec配置
UVa 1025 - A Spy in the Metro (White Book)