当前位置:网站首页>Sword finger offer 22 The penultimate node in the linked list
Sword finger offer 22 The penultimate node in the linked list
2022-06-27 16:58:00 【chenyson】
difficulty : Simple
The frequency of :81
subject : Enter a linked list , Output the last number in the list k Nodes . In order to conform to the habits of most people , From 1 Start counting , That is, the tail node of the list is the last 1 Nodes .
for example , A list has 6 Nodes , Start from the beginning , Their values, in turn, are 1、2、3、4、5、6. The last of the list 3 Each node has a value of 4 The node of .

Their thinking : Speed pointer
Be careful :
- while(p1!=null), Here is p1, instead of p1.next
Code :
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode p1=head;
ListNode p2=head;
while(k>0){
p1=p1.next;
k--;
}
while(p1!=null){
p1=p1.next;
p2=p2.next;
}
return p2;
}
}
边栏推荐
- 事件监听机制
- 【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。
- [the way of programmer training] - 3 Character count statistics
- Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
- Array represents a collection of several intervals. Please merge all overlapping intervals and return a non overlapping interval array. The array must exactly cover all the intervals in the input. 【Le
- LACP details
- 华为云DevCloud重磅发布四大新能力,创下国内两项第一
- EMQ helps Qingdao Yanbo build a smart water platform
- 2022年中国音频市场年度综合分析
- Construction and management practice of ByteDance buried point data flow
猜你喜欢
![[the way of programmer training] - 3 Character count statistics](/img/0c/e506571a4b22edc7c02a346909eeab.jpg)
[the way of programmer training] - 3 Character count statistics

Annual comprehensive analysis of China's audio market in 2022

List to table

10分钟掌握mysql的安装步骤

模拟进程调度
锚文本大量丢失的问题

Simulated process scheduling

ROS "topic" programming implementation

d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?

Handling method of occasional error reporting on overseas equipment
随机推荐
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
Adaoracle supports multi chain distributed Oracle with wide area node quotation
Awk determines whether the first column is greater than 1. If it is greater than 1, the row will be printed
Oracle concept 3
After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
Event listening mechanism
Delete duplicate elements in the sorting linked list
Cesium realizes satellite orbit detour
软件测试-测试的概念,单元测试的详细介绍,如何设计测试用例
National food safety risk assessment center: do not blindly and unilaterally pursue "zero addition" and "pure natural" food
防火墙基础之源NAT地址转换和服务器映射web页面配置
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
Mode setting of pulseaudio (21)
EMQ 助力青岛研博建设智慧水务平台
QT5 之信号与槽机制(信号与槽的基本介绍)
特殊函数计算器
关于#mysql#的问题:问题遇到的现象和发生背景
锚文本大量丢失的问题
树莓派初步使用
d3dx9_ How to repair 40.dll? Win10 system d3dx9_ What if 40.dll is lost?