当前位置:网站首页>Sword finger offer 22. the penultimate node in the linked list
Sword finger offer 22. the penultimate node in the linked list
2022-07-24 16:36:00 【ThE wAlkIng D】
Title Description

Problem analysis
Use the speed pointer and double pointer to do , Last but not least K It means that the fast and slow pointers are separated k Units to traverse
First, let the pointer go K A unit of ,while Loop through the fast pointer
Then let the slow pointer and the fast pointer go back together , Side by side while loop , Walking speed pointer .
( The title of the linked list is mostly to use while loop )
Code instance
/** * 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) {
ListNode fast = head;
ListNode slow = head;
while(fast != null&& k > 0){
fast = fast.next;
k--;
}
while(fast != null){
fast = fast.next;
slow = slow.next;
}
return slow;
}
}
边栏推荐
- Envi5.3 open GF-1 WFV data
- .net review the old and know the new: [6] what is LINQ
- GEO satellite data download
- Getting started with ARP
- 【技术】uniapp之聊天室 demo
- Codeforces round 690 (Div. 3) B. last year's substring conventional solution
- Zcmu--5023: family division (C language)
- 百度推广“删除重提”是什么意思?
- Cross server, insert, search
- 简单工厂模式都不会,你真应该去工厂搬砖!
猜你喜欢
随机推荐
QT design robot simulation controller -- key control robot joint rotation
剑指 Offer 22. 链表中倒数第k个节点
About SQL data query statements
Druid integration shardingsphere appears xxmapper Reasons and solutions of XML error reporting
GEO satellite data download
Custom view - Custom button
TCP protocol debugging tool tcpengine v1.3.0 tutorial
QT keyboard event (II) -- long press the key to trigger the event event repeatedly, and the problem is solved
Qt信号和槽连接失败原因及解决办法
[technology] chat room demo of uniapp
Meizu blood exchange: Alibaba quits? Zhuhai SASAC joins the Bureau, and Huang Zhang hands over the controlling stake! Li Nan is removed from the main staff!
1184. Distance between bus stops
解决Eureka默认缓存配置导致时效性问题
Qt键盘事件(二)——长按按键反复触发event事件问题解决
Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
随笔记:同步、异步和微任务、宏任务的打印顺序
Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images
hping3安装使用
Hping3 installation and use
After data management, the quality is still poor








