当前位置:网站首页>剑指 Offer 22. 链表中倒数第k个节点
剑指 Offer 22. 链表中倒数第k个节点
2022-07-24 16:31:00 【ThE wAlkIng D】
题目描述

问题解析
使用快慢指针双指针来做,倒数第K的意思就是快慢指针相隔k个单位进行遍历
首先先让快指针走K个单位,while循环遍历快指针
接着再让慢指针和快指针一起向后走,在并列一个while循环,走快慢指针。
(链表的题目多是使用while循环)
代码实例
/** * 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;
}
}
边栏推荐
- Princeton calculus reader 02 Chapter 1 -- composition of functions, odd and even functions, function images
- .NET 测试框架 xUnit,MSTest, Specflow 使用经验汇总
- EventLoop event loop mechanism
- Creation and inheritance of JS class
- What is the difference between cookies, localstorage and sessionstorage?
- Wentai technology and Baoxin software deepened 5g cooperation, and 5g manufacturing settled in Baowu, China
- Minor record
- 简单使用 MySQL 索引
- The 3D sensing market is accelerating. Who will be better, TOF or structured light?
- How to realize seamless rotation map? Write it again with native JS
猜你喜欢

Quickly view the version of redis in the server

Envi SHP to ROI and mask the grid

如何防止跨站点脚本 (XSS) 攻击完整指南

About SQL data query statements

124 maximum path sum in binary tree

Multithreading (basic)

Servlet framework (servlet+jsp) + addition, deletion, modification and query + paging implemented by MySQL (function package student information entry, addition, deletion, modification and query of st

C TCP client form application asynchronous receiving mode

Envi grid resampling

普林斯顿微积分读本02第一章--函数的复合、奇偶函数、函数图像
随机推荐
ZCMU--5023: 家庭划分(C语言)
[leetcode] skillfully use bit operation
Qt键盘事件(二)——长按按键反复触发event事件问题解决
How to deal with the start and end times in mybatics
Jenkins CLI 命令详解
Simple QQ? QT can also be achieved! (I)
MySQL write lock does not take effect
Qt键盘事件(一)——检测按键输入
Mobile phone comparison redmi note8 and realm x2
Code shoe set - mt2095 · zigzag jump
安全的证券公司有哪些?我想在手机上买股票
[technology] online seat selection demo of uniapp
QT keyboard event (I) -- detect key input
After data management, the quality is still poor
Analysis of double pointer sliding window method and solution of leetcode related problems
期盼已久全平台支持-开源IM项目OpenIM之uniapp更新
15. ARM embedded system: how to debug single board with PC
With notes: printing order of synchronous, asynchronous, micro task and macro task
Code shoe set - mt2093 · palindrome digit
Rest style