当前位置:网站首页>ACM. Hj51 outputs the penultimate node ●
ACM. Hj51 outputs the penultimate node ●
2022-06-21 21:33:00 【chenyfan_】
HJ51 Output the penultimate... In the one-way linked list k Nodes ●
describe
Enter a one-way linked list , Output the last number in the list k Nodes , The bottom of the list 1 The first node is the tail pointer of the linked list .
The definition of linked list node is as follows :
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
Return to the penultimate... Normally k individual Node pointer , Exception return Null pointer .
requirement :
(1) Build a linked list in positive order ;
(2) After construction Forget the length of the linked list .
Data range : The length of the linked list meets 1 ≤ n ≤ 1000 1 \le n \le 1000 1≤n≤1000, k ≤ n k \le n k≤n, The data in the linked list meets 0 ≤ v a l ≤ 10000 0 \le val \le 10000 0≤val≤10000
This topic has Multiple sets of sample input .
Input description :
Enter description
- Enter the number of linked list nodes
- Enter the value of the linked list
- Input k Value
Output description :
Output an integer
Example
Input :
8
1 2 3 4 5 6 7 8
4
Output :
5
Answer key
1. Speed pointer
Multiple sets of test cases , Output in each cycle cout that will do .
Use the speed pointer to count down k The judgment of number :
A fast pointer is faster than a slow pointer k Step , When the fast pointer comes null Node time , The slow pointer now points to the penultimate k Number .
#include <iostream>
#include <vector>
using namespace std;
struct ListNode
{
int m_nKey;
ListNode* m_pNext;
};
int main(){
int n, k, num;
while(cin >> n){
// Number of nodes for multiple groups of use cases
ListNode* dummyHead = new ListNode(); // Virtual head node
ListNode* pre = dummyHead;
for(int i = 0; i < n; ++i){
// Create a one-way linked list
cin >> num;
ListNode* curr = new ListNode();
pre->m_pNext = curr;
curr->m_nKey = num;
pre = curr;
}
cin >> k;
ListNode *slow = dummyHead->m_pNext; // Speed pointer
ListNode *fast = dummyHead->m_pNext;
if(k == 0){
// Last but not least 0 Number , prune
cout << 0 << endl;
}else{
for(int i = 0; i < k; ++i){
// A fast pointer is faster than a slow pointer k Step
fast = fast->m_pNext;
}
while(fast != nullptr){
// When the fast pointer comes null Node time
slow = slow->m_pNext;
fast = fast->m_pNext;
}
cout << slow->m_nKey << endl; // Output slow node
}
}
return 0;
}
边栏推荐
- Why does defer not execute after the program exits
- ACM. HJ51 输出单向链表中倒数第k个结点 ●
- LVS+Keepalived高可用群集实战部署
- 混合云演习常见案例
- Caricature scientifique | Vous pouvez apprendre l'EEG en regardant les images. Voulez - vous essayer?
- cv/nlp哪些小方向好发论文?
- 英文论文的proposal怎么写?
- 提升四大性能!D-Wave发布下一代量子退火机原型
- K - Clairewd’s message HDU - 4300 (EXKMP)
- ctfshow 105-127
猜你喜欢

Citus 11 for Postgres 完全开源,可从任何节点查询(Citus 官方博客)

用keil 5编译C51时出现定义未使用的处理方法

集群一---LVS负载均衡集群NAT模式及LVS负载均衡实战部署

Merge two ordered arrays

2022年最新河南建筑施工电工(建筑特种作业)模拟试题及答案

PowerPoint tutorial, how to organize slides into groups in PowerPoint?

ASP. Net core creates razor page and uploads multiple files (buffer mode)

30组户外旅行游玩VLOG记录LUTs调色预设Moody Travel LUTs

libtorch显存管理示例

Database management: Navicat premium 15
随机推荐
cv/nlp哪些小方向好发论文?
Citus 11 for Postgres is completely open source and can be queried from any node (citus official blog)
NewOJ Week 6
JS里的数据类型(基础)
What are some tricks that novice programmers don't know?
Tencent global digital ecology Conference - high speed intelligent computing special session!
请问一下,大学生查文献在哪个网站比较好呀?
2022年焊工(高级)考试题模拟考试题库模拟考试平台操作
C语言数组与指针练习题(原题+解析+原码)
The first in the industry! Krypton app has obtained the authoritative certification of China Network Security Review Technology and Certification Center
Basic rules of smiles
New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
2022 National latest fire facility operator (intermediate fire facility operator) simulation question bank and answers
Xmind8 最新破解教程(亲测有用)
Vscode有什么好用的插件?
[server data recovery] a case of RAID5 data recovery of an EMC server
关于SQL Server中变量前加上 N与其他使用情况解析
Hypebeast, un média à la mode, est sur le point d'être lancé: 530 millions de dollars pour le troisième trimestre
有哪些将英文文献翻译为中文的网站或软件?
Mysql database - index