当前位置:网站首页>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;
}
}
边栏推荐
- 链表中倒数第k个节点——双指
- Install pycharm
- How low-end computers learn secrets in depth - using the mistgpu computing platform
- Use of C3d
- Current limiting ratelimiter of guava
- 合并两个排序的链表——每日两题
- Activate function
- 【google】解决google浏览器不弹出账号密码保存框且无法保存登录信息问题
- DNA修饰金属锇Os纳米颗粒OsNPS-DNA|DNA修饰金属铱纳米颗粒IrNPS-DNA
- User mode vs kernel mode, process vs thread
猜你喜欢

隔离级别RR、间隙锁、幻读

EMC问题的根源在哪?

删除链表中的节点——每日一题

Introduction to magnetic ring selection and EMC rectification skills
![[JVM optimization] online JVM tuning practice](/img/e3/5fa128805af0ca03f0b6715b78d398.jpg)
[JVM optimization] online JVM tuning practice

Retryer of guava

How to understand CMS collector to reduce GC pause time

软考证书还能这样用!拿到证书=获得职称?

guava之EventBus

(daily question) - the longest substring without repeated characters
随机推荐
铜铟硫CuInSe2量子点修饰DNA(脱氧核糖核酸)DNA-CuInSe2QDs(齐岳)
EMC rectification method set
一口气学完4种 Redis 集群方案,真是各有千秋
Industry standards and certification of common electronic products
调整数组顺序使奇数位于偶数前面——每日两题
DNA修饰贵金属纳米颗粒|DNA脱氧核糖核酸修饰金属钯Pd纳米颗粒PdNPS-DNA
常用电子产品行业标准及认证
面试中必不可少的性能优化专题~
build_ opencv.sh
Install pycharm
软考证书还能这样用!拿到证书=获得职称?
MHA high availability configuration and failover
Don't be afraid of ESD static electricity. This article tells you some solutions
ESD防护为何对集成电路如此重要?又该如何防护?
【google】解决google浏览器不弹出账号密码保存框且无法保存登录信息问题
微信小程序隐藏滚动条的方法
Redis的RDB持久化和AOF持久化的底层原理
高性能内存队列-Disruptor
Daily question - split equal sum subset
Deeply analyze the implementation of singleton mode