当前位置:网站首页>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;
}
}
边栏推荐
- 整改了七次,花了半个月时间,惨痛的EMC总结
- 浅谈深分页问题
- [solution] visual full link log tracking - log tracking system
- Install pycharm
- 微信小程序隐藏滚动条的方法
- How to connect the uniapp project to the real mobile phone for debugging
- EMC rectification ideas
- 4.1.4为什么要将成员变量设置为private
- 删除链表中的节点——每日一题
- Collection | combined with my personal experience, I have summarized these seven EMC related knowledge
猜你喜欢

Deeply analyze the implementation of singleton mode

Essential performance optimization topics in the interview~

guava之EventBus

“核弹级” Log4j 漏洞仍普遍存在,并造成持续影响

【着色器实现Negative反色效果_Shader效果第十一篇】

Learn software testing in two weeks? I was shocked!

EMC rectification ideas

EMC整改方法集合

Advanced pointer practice

Which of class A and class B is more stringent in EMC?
随机推荐
【jvm优化超详细】常见的JVM调优场景
guava之EventBus
MySQL字段 不推荐使用 Null 的理由
Shortest seek time first (SSTF)
Student duty problems
Basic usage and precautions of arrow function (= >) and three-point operator (...) in ES6 (this points to)
ThreadLocal那些事
High response ratio first
【干货】32个EMC标准电路分享!
项目经历总结
High performance memory queue -disruptor
短作业优先SJF
收藏 | 结合个人经验,我总结了这7点EMC相关知识
面试中必不可少的性能优化专题~
软考证书还能这样用!拿到证书=获得职称?
5g commercial third year: driverless "going up the mountain" and "going to the sea"
ESD静电不用怕,本文告诉你一些解决方法
高响应比优先
JS upload file method
Deeply analyze the implementation of singleton mode