当前位置:网站首页>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;
}
}
边栏推荐
- 锚文本大量丢失的问题
- 当发布/订阅模式遇上.NET
- Synchronization mechanism of dual namenodes
- Domain name binding dynamic IP best practices
- 华为云DevCloud重磅发布四大新能力,创下国内两项第一
- d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
- Leetcode daily practice (sum of two numbers)
- 【多线程】线程通信调度、等待集 wait() 、notify()
- Redis Series 2: data persistence improves availability
- wheel ui
猜你喜欢

d3dx9_ How to repair 38.dll? d3dx9_ 38. How to download a missing DLL?

10 minutes to master the installation steps of MySQL
P.A.R.A 方法在思源的简易应用(亲测好用)

Defiato is an innovation that combines user-friendly features of a centralized platform with defi services

2022年中国音频市场年度综合分析

米哈游起诉五矿信托,后者曾被曝产品暴雷

继手机之后 报道称三星也削减了电视等家电产品线的产量

d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download

Bit. Store: long bear market, stable stacking products may become the main theme

Community sharing jumpserver in the eyes of senior open source users: a fortress machine for "Crazy" iteration
随机推荐
QT5 之信号与槽机制(信号与槽的基本介绍)
当发布/订阅模式遇上.NET
Redis Series 2: data persistence improves availability
Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
Mihayou sued Minmetals trust, which was exposed to product thunderstorms
Oracle概念三
Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
Impressive questions
Adaoracle supports multi chain distributed Oracle with wide area node quotation
智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
【多线程】线程通信调度、等待集 wait() 、notify()
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
2/15 topology sorting +dfs (the order of specified directions is very important) +bfs
Mode setting of pulseaudio (21)
Drawing for example study of flashcc
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
Mobile terminal click penetration
事务的隔离级别详解
Handling method of occasional error reporting on overseas equipment
QT5 之信号与槽机制(演示控件自带的信号与槽函数关联)