当前位置:网站首页>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;
}
}
边栏推荐
- LACP details
- QT audio playback upgrade (7)
- Practice of constructing ten billion relationship knowledge map based on Nebula graph
- Qt5 signal and slot mechanism (basic introduction to signal and slot)
- Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
- Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
- Simulated process scheduling
- Oracle概念三
- Related configuration commands of Huawei LACP
- Handwritten promise series - all
猜你喜欢

Leetcode daily practice (main elements)

字节跳动埋点数据流建设与治理实践

一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】
A large number of missing anchor text

LeetCode每日一练(两数之和)

防火墙基础之源NAT地址转换和服务器映射web页面配置

Unity 阴影——阴影平坠(Shadow pancaking)
Yyds dry inventory brief chrome V8 engine garbage collection

What is the level 3 password complexity of ISO? How often is it replaced?

Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
随机推荐
鸿蒙发力!HDD杭州站·线下沙龙邀您共建生态
Leetcode 5. Longest Palindromic Substring
字节跳动埋点数据流建设与治理实践
Bit. Store: long bear market, stable stacking products may become the main theme
Byte + Google super full kotlin learning King fried notes! Kotlin introductory tutorial + Advanced kotlin enhanced actual combat (with demo)
Unity 阴影——阴影平坠(Shadow pancaking)
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
[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
Leetcode daily practice (sum of two numbers)
d3dx9_ How to repair 32.dll? d3dx9_ Solution to 32.dll missing
EMQ helps Qingdao Yanbo build a smart water platform
Special function calculator
基于 Nebula Graph 构建百亿关系知识图谱实践
[multithreading] thread communication scheduling, waiting set wait(), notify()
Handling method of occasional error reporting on overseas equipment
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
Why should string be designed to be immutable?
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download
How to modify / display GPIO status through ADB shell