当前位置:网站首页>[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
[Brave food, not afraid to write the linked list] The problem of the penultimate node of the linked list
2022-08-10 11:09:00 【rice, rice】
前言
本系列主要整理面试中需要掌握的手撕代码题.This section describes the linked list node from bottom problem.
一、BM8 链表中倒数最后k个结点

(1)定义快慢指针,首先让快指针走k个位置;
(2)How quickly and then let the pointer to move at the same time,When at the end of a pointer to the end,慢指针的位置就是倒数第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)First, use the method to find the need to delete the node,Note is positioning to delete a node before;
(2)To determine whether to delete the first node,如果是,直接返回头节点的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
}
边栏推荐
- Redis6(一)——NoSQL数据库简介与Redis的安装
- 首次入选OSDI顶会!腾讯提出超大规模推荐系统的模型低延时更新方案
- GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
- 「时序数据库」使用cassandra进行时间序列数据扫描
- 2022年裁员潮,失业程序员何去何从?
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- 【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
- 企业如何判断数据治理是否成功?
- runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
- HDU 1520 Anniversary party (tree dp)
猜你喜欢

开发模式对测试的影响

【电商运营】你真的了解社交媒体营销(SMM)吗?

EasyCVR级联时,修改下级平台名称将不同步至上级平台

Short video software development - how to break the platform homogenization

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

mysql5.7 installation and deployment - yum installation

第2章-矩阵及其运算-矩阵创建(1)

谷歌数据中心发生“电力事故”造成 3 人受伤

2022年裁员潮,失业程序员何去何从?

「业务架构」介绍BPMN第二部分-泳道
随机推荐
getParameter()与 getAttribute()的用法与区别
SQL与NoSQL最终会走向融合吗?
what is rtems
Several small projects that I have open sourced over the years
杭电多校-Loop-(不确定性贪心+线段树)
Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
TCP/IP笔记
【无标题】
【Azure云】服务端点和私有链接有什么区别?观点(1)
Get started quickly and conquer three different distributed architecture calling schemes
Chapter 22 Source Code File REST API Reference (4)
js猜拳小游戏源码
「时序数据库」使用cassandra进行时间序列数据扫描
从产品维度来看 我们为什么不能完全信任Layer2?
"MySQL Advanced Chapter" 6. Principles of index creation and design
OneFlow源码解析:算子指令在虚拟机中的执行
Load balancing principle analysis and source code interpretation
Cybersecurity Notes 5 - Digital Signatures
Mobile and PC compatible loading and toast message plugins
Redis(三)——配置文件详解、发布和订阅、新数据类型