当前位置:网站首页>[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
}
边栏推荐
- 使用cpolar远程连接群晖NAS(升级固定链接2)
- 【勇敢饭饭,不怕刷题之链表】有序链表的合并
- 高阶组件使用
- 4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
- 网络文化经营许可证
- js guessing game source code
- Weilai-software development engineer side record
- 程序员追求技术夯实基础学习路线建议
- [Azure Cloud] What is the difference between a service endpoint and a private link?point of view (1)
- 面试官:项目中 Dao、Service、Controller、Util、Model 怎么划分的?
猜你喜欢
2023版揽胜运动曝光,安全、舒适一个不落
从产品角度看 L2 应用:为什么说这是一个游乐场?
GPU accelerated Pinterest recommendation model, the number of parameters increased by 100 times, and the user activity increased by 16%
C#实战:基于ItextSharp技术标签生成小工具
Cybersecurity Notes 5 - Digital Signatures
bus event bus use
ISO9001在讲什么?过程方法和风险思维
蔚来-软件开发工程师一面记录
商城限时秒杀功能系统
HCIP ---- VLAN
随机推荐
What is an abstract class
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
网络文化经营许可证
mysql5.7 installation and deployment - yum installation
「时序数据库」使用cassandra进行时间序列数据扫描
4 面拿华为 offer 的水平,面试阿里居然一面就被吊打?
OneFlow源码解析:算子指令在虚拟机中的执行
关于“码农”的一点自嘲解构
SQL与NoSQL最终会走向融合吗?
从产品角度看 L2 应用:为什么说这是一个游乐场?
GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
"MySQL Advanced Chapter" 6. Principles of index creation and design
CodeChef STMRRG String Merging (dp)
Codeforces 814 C. An impassioned circulation of affection (dp)
In August the DB list latest scores - database Engines
HDU 1520 Anniversary party (tree dp)
From the product dimension, why can't we fully trust Layer2?
2022.8.8-----leetcode.761
什么是抽象类
Redis (six) - transaction and lock mechanism of Redis6 (unfinished, to be supplemented)