当前位置:网站首页>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;
}
}
边栏推荐
- How to modify / display GPIO status through ADB shell
- 鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
- 06. First introduction to express
- Hongmeng makes efforts! HDD Hangzhou station · offline salon invites you to build ecology
- C language teacher workload management system
- Source NAT address translation and server mapping web page configuration of firewall Foundation
- Raspberry pie preliminary use
- 锚文本大量丢失的问题
- Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
- Event listening mechanism
猜你喜欢
Defiato is an innovation that combines user-friendly features of a centralized platform with defi services
Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
Determine the maximum number of specific words in a string
Drawing for example study of flashcc
d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
当发布/订阅模式遇上.NET
Oracle概念二
Why should string be designed to be immutable?
Ti Click: quickly set up tidb online laboratory through browser | ti- team interview can be conducted immediately
Autodesk NavisWorks 2022 software installation package download and installation tutorial
随机推荐
Leetcode 5. Longest Palindromic Substring
Taishan Office Technology Lecture: the first difficulty is vertical positioning
Autodesk Navisworks 2022软件安装包下载及安装教程
Missing d3d10 How to repair DLL files? Where can I download d3d10.dll
Unity shadow shadow pancaking
Open source 23 things shardingsphere and database mesh have to say
LeetCode每日一练(两数之和)
郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮
Source NAT address translation and server mapping web page configuration of firewall Foundation
Drawing for example study of flashcc
Regular matching starts with what, ends with what, starts with what, and ends with what
C système de gestion de la charge de travail des enseignants en langues
【牛客刷题】NowCoder号称自己已经记住了1-100000之间所有的斐波那契数。 为了考验他,我们随便出一个数n,让他说出第n个斐波那契数。如果第n个斐波那契大于6位则只取后6位。
Determine the maximum number of specific words in a string
树莓派初步使用
Halcon: discrete digital OCR recognition
Realize simple three-D cube automatic rotation
C语言教师工作量管理系统
What is the level 3 password complexity of ISO? How often is it replaced?
LeetCode 124. Binary tree maximum path sum - binary tree series question 8