当前位置:网站首页>The penultimate node in the linked list - Double finger
The penultimate node in the linked list - Double finger
2022-07-28 07:35:00 【Ink man bookworm】
The finger of the sword Offer 22. Last in the list k Nodes
The difficulty is simple 376
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 .
Example :
Given a linked list : 1->2->3->4->5, and k = 2.
Back to the list 4->5.
Pass times 374,318
Submit the number 466,479
Ideas
Use double pointers to solve the problem , First define the speed pointer , Let the quick pointer go first
kStep , Finally, walk with the slow pointer , Until the end , Returns the full pointerBrushing questions makes me happy
The title algorithm code is as follows
class Solution {
public ListNode getKthFromEnd(ListNode head, int k) {
ListNode slow =head,fast=head;
while (fast!=null&&k>0){
fast=fast.next;
k--;
}
// Went together
while (fast!=null){
slow=slow.next;
fast=fast.next;
}
return slow;
}
}
边栏推荐
- Date n days ago
- EMC设计攻略 —时钟
- 2018-cvpr-Gesture Recognition: Focus on the Hands
- Eventbus of guava
- Summary of RFID radiation test
- Learn software testing in two weeks? I was shocked!
- How to understand CMS collector to reduce GC pause time
- Daily question - split equal sum subset
- Current limiting ratelimiter of guava
- How did tensor leak your memory / video memory
猜你喜欢

DNA脱氧核糖核酸修饰金属铂纳米颗粒PtNPS-DNA|科研试剂

最近最久未使用

Install pycharm

DNA修饰金属铑Rh纳米颗粒RhNPS-DNA(DNA修饰贵金属纳米颗粒)

Learning to estimate 3D hand pose from single RGB image & amp notes

【干货】32个EMC标准电路分享!

EMC's "don't come back until you rectify"

Retryer of guava

Soft exam certificate can be used like this! Get a certificate = get a professional title?

After learning the four redis cluster solutions at one go, each has its own merits
随机推荐
短作业优先SJF
Shortest seek time first (SSTF)
flow_ x+flow_ y---RGB
LeNet5、AlexNet、VGGNet、ResNet
链表中倒数第k个节点——双指
DNA脱氧核糖核酸修饰金属铂纳米颗粒PtNPS-DNA|科研试剂
The cornerstone of EMC - complete knowledge of electromagnetic compatibility filtering!
常用电子产品行业标准及认证
Date n days ago
如何理解CMS回收器降低gc停顿时间
牛客Mysql——SQL必知必会
每日一题——分割等和子集
uniapp 移动端 两种横竖屏切换方案
build_ opencv.sh
C语言详解系列——数组详解,一维数组、二维数组
Tutorial (7.0) 06. Zero trust network access ztna * forticlient EMS * Fortinet network security expert NSE 5
【解决方案】可视化全链路日志追踪-日志追踪系统
CAS vs Database optimistic lock
大话持久性与redolog
面试中必不可少的性能优化专题~