当前位置:网站首页>Sword finger offer 22 The penultimate node in the linked list - speed pointer
Sword finger offer 22 The penultimate node in the linked list - speed pointer
2022-04-23 17:33:00 【hequnwang10】
One 、 Title Description
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 1:
Given a linked list : 1->2->3->4->5, and k = 2.
Back to the list 4->5.
Two 、 Problem solving
Speed pointer
There is a difference between the speed pointer and the slow pointer K Nodes , Then update the speed pointer
/** * 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) {
// Speed pointer
if(head == null){
return head;
}
ListNode fast = head;
ListNode slow = head;
for(int i = 0;i<k;i++){
fast = fast.next;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
版权声明
本文为[hequnwang10]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231732009222.html
边栏推荐
- El date picker limits the selection range from the current time to two months ago
- ClickHouse-数据类型
- 2.Electron之HelloWorld
- 958. 二叉树的完全性检验
- Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
- C语言函数详解
- Advantages and disadvantages of several note taking software
- Header built-in object
- Detailed explanation of C webpai route
- 41. The first missing positive number
猜你喜欢

C# Task. Delay and thread The difference between sleep

Deep understanding of control inversion and dependency injection

Why do some people say SCM is simple and I have to learn it so hard?
![[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof](/img/71/14a17128dbe0f02edb4db3da479ef2.jpg)
[logical fallacy in life] Scarecrow fallacy and inability to refute are not proof

92. Reverse linked list II byte skipping high frequency question

Net standard

Future 用法详解

Advantages and disadvantages of several note taking software

Perception of linear algebra 2

Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
随机推荐
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
Understanding of RPC core concepts
ECMAScript history
Self use learning notes - connectingstring configuration
Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
Ouvrir des contrats à terme, ouvrir des comptes en nuage ou faire confiance aux logiciels des sociétés à terme?
[simple understanding of database]
Halo 开源项目学习(二):实体类与数据表
402. 移掉 K 位数字-贪心
圆环回原点问题-字节跳动高频题
flink 学习(十二)Allowed Lateness和 Side Output
For the space occupation of the software, please refer to the installation directory
Shell - introduction, variables, and basic syntax
[difference between Oracle and MySQL]
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Metaprogramming, proxy and reflection
Devexpress GridView add select all columns
958. 二叉树的完全性检验
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
开期货,开户云安全还是相信期货公司的软件?