当前位置:网站首页>[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
}
边栏推荐
- Taro小程序跨端开发入门实战
- The usage and difference between getParameter() and getAttribute()
- CodeChef STRMRG String Merging (dp)
- Some tips for using Unsafe
- what is bsp in rtems
- CodeChef STMRRG String Merging (dp)
- 8月份DB-Engines 数据库排行榜最新战况
- AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
- Codeforces 814 C. An impassioned circulation of affection (dp)
- 谷歌数据中心发生“电力事故”造成 3 人受伤
猜你喜欢

What is an abstract class

第3章-线性方程组(3)

《MySQL高级篇》六、索引的创建与设计原则

商城限时秒杀功能系统

【勇敢饭饭,不怕刷题之链表】有序链表的合并

Text selection rounded style border-radius

Gold, nine, silver and ten job-hopping seasons: technical interview questions and answers on Alibaba, Baidu, JD.com, and Meituan

Unsafe的一些使用技巧

AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)

runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
随机推荐
关于“码农”的一点自嘲解构
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%
Redis6(一)——NoSQL数据库简介与Redis的安装
EasyCVR级联时,修改下级平台名称将不同步至上级平台
杭电多校-Loop-(不确定性贪心+线段树)
PPT | 「课件」企业中高层人员安全管理培训(118页)
getParameter()与 getAttribute()的用法与区别
越折腾越好用的 3 款开源 APP
Research on motion capture system for indoor combined positioning technology
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
Interviewer: Dao, in Service, the Controller, Util, divided into the Model?
owl.carousel海报卡片Slider轮播切换
Memory problems difficult to locate, it is because you do not use ASAN
第2章-矩阵及其运算-矩阵创建(1)
谷歌数据中心发生“电力事故”造成 3 人受伤
让软件飞——“X+”技术揭秘
Regarding the missing json converter, the error message is: No converter found for return value of type
《MySQL高级篇》六、索引的创建与设计原则
3D rotating text animation js special effects