当前位置:网站首页>【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
2022-08-10 10:41:00 【饭啊饭°】
前言
本系列主要整理面试中需要掌握的手撕代码题。本节介绍链表倒数节点问题。
一、BM8 链表中倒数最后k个结点

(1)定义快慢指针,首先让快指针走k个位置;
(2)然后让快慢指针同时移动,当快指针到末尾的时候,慢指针的位置就是倒数第k个。
function FindKthToTail( pHead , k ) {
// write code here
var fast = pHead;
var slow = pHead;
for(var i=0;i<k;i++){
if(fast==null){
return null
}
fast = fast.next;
}
while(fast){
fast = fast.next;
slow = slow.next;
}
return slow
}
二、BM9 删除链表的倒数第n个节点

(1)首先用上题的方法找到需要删除的节点,注意的就是定位到需要删除的前一个节点;
(2)判断要删除的是否是第一个节点,如果是,直接返回头节点的next;
(3)如果不是,则将该节点的next的next赋值给next。
function removeNthFromEnd( head , n ) {
// write code here
var slow = head;
var fast = head;
for(var i=0;i<n;i++){
if(fast == null){
return head
}
fast = fast.next
}
if(fast == null){
return head.next
}
while(fast.next!=null){
slow = slow.next;
fast = fast.next;
}
slow.next = slow.next.next
return head
}
边栏推荐
- 8月份DB-Engines 数据库排行榜最新战况
- Store limited time seckill function system
- mysql5.7安装部署-yum安装
- 兼容移动和PC的loading加载和toast消息插件
- 4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
- Regarding the missing json converter, the error message is: No converter found for return value of type
- Text selection rounded style border-radius
- Taro小程序跨端开发入门实战
- chart.js水平柱状图插件
- FastReport.Net 2022.2.17 Crack
猜你喜欢

GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%

Redis(三)——配置文件详解、发布和订阅、新数据类型

OneFlow源码解析:算子指令在虚拟机中的执行

交换 生成树 知识总结

Short video software development - how to break the platform homogenization

Redis(六)——Redis6的事务和锁机制(未完成,待补)

STM32 encapsulation ESP8266 a key configuration function: implementations of AP mode and the STA mode switch, server and the client to create

文本选中圆角样式border-radius

3D旋转文本动画js特效
![[Concept of Theory of Knowledge]](/img/c1/8dcc19e43f78e999b13c8da1c19f62.png)
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
随机推荐
高阶组件使用
Mount [shell][mount -o loop]
解决ASP.NET Core在Task中使用IServiceProvider的问题
Get started quickly and conquer three different distributed architecture calling schemes
【Azure云】服务端点和私有链接有什么区别?观点(1)
SQL中的字符串截取函数
Redis(三)——配置文件详解、发布和订阅、新数据类型
PPT | 「课件」企业中高层人员安全管理培训(118页)
database transaction
Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
ZZULIOJ 1124: Merge two sorted arrays
什么是抽象类
MongoDB数据库笔记
Codeforces 814 C. An impassioned circulation of affection (dp)
ZZULIOJ 1116 删除元素【删】
「首席工程师」首席(Principal )工程师修炼之道
OneFlow source code parsing: operator instructions executed in a virtual machine
2022.8.8-----leetcode.761
短视频软件开发——平台同质化如何破局
Will SQL and NoSQL eventually converge?