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

微信公众号正式环境上线部署,第三方公众平台接入

51单片机学习笔记(2)

27 classification of selectors

Syntax summary of easygui

L1和L2正则化

Leetcode-198- house raiding

Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud

006 operator introduction

Sudo rosdep init error ROS installation problem solution

阿里云安装MYSQL5.7
随机推荐
Dpkg package download addresses of various platforms (including arm64)
27 classification of selectors
filters获取data中的数据;filters使用data中的数据
[C题目]力扣876. 链表的中间结点
基于浏览器的分屏阅读
Idea error failed to determine a suitable driver class
国联证券买股票开户安全吗?
没错,请求DNS服务器还可以使用UDP协议
39 简洁版小米侧边栏练习
Development of uni app offline ID card identification plug-in based on paddleocr
I2C设备驱动程序的层次结构
Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud
Realsense-Ros安装配置介绍与问题解决
spark参数调整调优
基于AMD EPYC服务器的EDA芯片设计解决方案
How many ways can you assign initial values to a two-dimensional array?
PHP implements non blocking (concurrent) request mode through native curl
Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
37 元素模式(行内元素,块元素,行内块元素)
冈萨雷斯 数字图像处理 第一章绪论