当前位置:网站首页>面试题 02.02. 返回倒数第 k 个节点
面试题 02.02. 返回倒数第 k 个节点
2022-06-11 08:55:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:面试题 02.02. 返回倒数第 k 个节点
描述:实现一种算法,找出单向链表中倒数第 k 个节点。返回该节点的值。
解题思路
1、快慢指针;
原代码##
class Solution {
public:
int kthToLast(ListNode* head, int k) {
ListNode *slow = head, *fast = head;
for (int i = 0; i < k; ++i) {
fast = fast->next;
}
while (fast) {
fast = fast->next;
slow = slow->next;
}
return slow->val;
}
};
边栏推荐
猜你喜欢

Sword finger offer 40 Minimum number of K

The interviewer asked four questions and summed up four experiences

显示器要申请BS 476-7 怎么送样?跟显示屏一样吗

leetcode - 518. 零钱兑换 II

ActiveMQ simple tutorial, suitable for beginners, learning notes yyds

Sword finger offer 51 Reverse pair in array

标准化编写知识

Matlab r2022a installation tutorial

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

Introduction to knowledge atlas -- yedda annotation
随机推荐
Analysis of EN 45545 R24 oxygen index test method
EN 45545-2:2020 T11烟毒性检测
83. 删除排序链表中的重复元素
MySQL核心点笔记
【C语言-指针进阶】挖掘指针更深一层的知识
K8S应用(四)—— 搭建redis5 集群(可供外部直接访问)
硅树脂油漆申请美国标准UL 790 Class A 合适吗?
PHP解决中文显示乱码
LiveData 与 StateFlow,我该用哪个?
九九乘法表
[node] NPM part
【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程
SAP 物料主数据归档
What are the requirements of the drive housing UL 2043?
CodeTop - 排序奇升偶降链表
GCC AVR (ATMEL studio+ AVR studio) how to define the structure array in the program memory (flash) space and read it
面试官问了四个问题,总结了4个经验
显示屏DIN 4102-1 Class B1防火测试要求
leetcode - 739. 每日温度
En45545-2 R26 vertical combustion test introduction