当前位置:网站首页>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
边栏推荐
- SiteServer CMS5. 0 Usage Summary
- 92. Reverse linked list II byte skipping high frequency question
- Oninput one function to control multiple oninputs (take the contents of this input box as parameters) [very practical, very practical]
- 958. 二叉树的完全性检验
- 2. Electron's HelloWorld
- How to sort the numbers with text in Excel from small to large instead of the first number
- 圆环回原点问题-字节跳动高频题
- ClickHouse-表引擎
- Websocket (basic)
- How does matlab draw the curve of known formula and how does excel draw the function curve image?
猜你喜欢
Using quartz under. Net core -- operation transfer parameters of [3] operation and trigger
Qt error: /usr/bin/ld: cannot find -lGL: No such file or directory
MySQL installation
JVM class loading mechanism
Using quartz under. Net core -- general properties and priority of triggers for [5] jobs and triggers
Advantages and disadvantages of several note taking software
C# Task. Delay and thread The difference between sleep
ASP. Net core dependency injection service life cycle
01-初识sketch-sketch优势
Qt 修改UI没有生效
随机推荐
SiteServer CMS5. 0 Usage Summary
ClickHouse-SQL 操作
Sword finger offer 03 Duplicate number in array
386. 字典序排数(中等)-迭代-全排列
402. 移掉 K 位数字-贪心
Further optimize Baidu map data visualization
470. 用 Rand7() 实现 Rand10()
Construction of functions in C language programming
tidb-server 的配置文件在哪里?
JVM类加载机制
Using quartz under. Net core -- a simple trigger of [7] operation and trigger
练习:求偶数和、阈值分割和求差( list 对象的两个基础小题)
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
[difference between Oracle and MySQL]
RPC核心概念理解
Manually implement simple promise and its basic functions
[batch change MySQL table and corresponding codes of fields in the table]
Understanding and small examples of unity3d object pool
Change Oracle to MySQL
92. Reverse linked list II byte skipping high frequency question