当前位置:网站首页>[C topic] the penultimate node in the Niuke linked list
[C topic] the penultimate node in the Niuke linked list
2022-07-25 14:50:00 【GLC8866】
Ideas : Mark two pointers aim and cur from pListhead Start , Let the previous pointer first cur Move in advance k Nodes , Move the two pointers synchronously , Until the front cur Pointer for NULL,aim The position of the pointer is the penultimate k individual .
(IO type OJ topic , It can be improved step by step through the test cases he gives .)
The test case :
1,{1,2,3,4,5}
5,{1,2,3,4,5}
100,{}
6,{1,2,3,4,5}
0,{1,2,3,4,5}
10,{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}Expected output :
{5}
{1,2,3,4,5}
{}
{}
{}
{11,12,13,14,15,16,17,18,19,20}
struct ListNode* FindKthToTail(struct ListNode* pListHead, int k )
{
if(pListHead==NULL||k==0)// Empty linked list or k be equal to 0 Return to the empty linked list
return NULL;
struct ListNode* aim=pListHead;
struct ListNode* cur=pListHead;
while(k>0)//k Indicates the number of times the front pointer needs to be moved
{
if(cur==NULL)// once cur Equal to null pointer must terminate loop
break;
cur=cur->next;
k--;// Move once ,k Minus one .
}
if(k>0)//k A positive number means break Out of circulation , It indicates that the number of nodes has exceeded , Returns the entire list .
return NULL;
while(cur)// The most common situation , Double pointers move side by side , until cur by NULL.
{
cur=cur->next;
aim=aim->next;
}
return aim;
}
边栏推荐
- 32 use of chrome debugging tools
- 关于RDBMS和非RDBMS【数据库系统】
- Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
- How to use the random number function of JMeter
- GameFramework制作游戏(二)制作UI界面
- Thymeleaf controls whether display is displayed through style
- 快速搭建Dobbo小Demo
- Realsense-Ros安装配置介绍与问题解决
- awk从入门到入土(21)awk脚本调试
- 39 简洁版小米侧边栏练习
猜你喜欢

Ssh server rejected password

32 chrome调试工具的使用

37 元素模式(行内元素,块元素,行内块元素)

Browser based split screen reading

Wechat official account official environment online deployment, third-party public platform access

The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%

基于AMD EPYC服务器的EDA芯片设计解决方案

Products on Apple's official website can save 600 yuan by buying iPhone 13 Pro max at a discount

Go语言创始人从Google离职

41 图片背景综合-五彩导航图
随机推荐
MySQL 45讲 | 06 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
(原创)自定义一个滚屏的RecyclerView
44 新浪导航 ,小米边栏 练习
39 simple version of millet sidebar exercise
QObject源码剖析-d指针和q指针
32 use of chrome debugging tools
Educational codeforces round 132 (rated for Div. 2) C, d+ac automata
Resource not found: rgbd_launch 解决方案
awk从入门到入土(21)awk脚本调试
Educational Codeforces Round 132 (Rated for Div. 2) C,D+AC自动机
Is it safe for Guolian securities to buy shares and open an account?
Overview of cloud security technology development
Gameframework making games (II) making UI interface
L1 and L2 regularization
Dpkg package download addresses of various platforms (including arm64)
51 single chip microcomputer learning notes (1)
Ten common application scenarios of redis
【MySQL必知必会】触发器 | 权限管理
Awk from entry to earth (24) extract the IP of the instruction network card
转载----如何阅读代码?