当前位置:网站首页>[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
}
边栏推荐
- Store limited time seckill function system
- Automated Testing and Selenium
- 文本选中圆角样式border-radius
- 【Azure云】服务端点和私有链接有什么区别?观点(1)
- Break through the dimensional barriers and let the dolls around you move on the screen!
- 是什么影响了MySQL性能?
- 2022年裁员潮,失业程序员何去何从?
- Redis设计与实现
- chart.js horizontal column chart plugin
- Double.doubleToLongBits()方法使用
猜你喜欢
ISO9001在讲什么?过程方法和风险思维
"MySQL Advanced Chapter" 6. Principles of index creation and design
让软件飞——“X+”技术揭秘
Redis6(一)——NoSQL数据库简介与Redis的安装
Short video software development - how to break the platform homogenization
第2章-矩阵及其运算-矩阵创建(1)
"Chief Engineer" Principal (Principal) engineer's way of training
"Data Strategy" Results-Driven Enterprise Data Strategy: Organization and Governance
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
越折腾越好用的 3 款开源 APP
随机推荐
CodeChef STRMRG String Merging (dp)
AUTOCAD——减少样条曲线控制点数、CAD进阶练习(三)
POJ 1026 Cipher (置换群)
Research on motion capture system for indoor combined positioning technology
技能大赛训练题:组策略一
第2章-矩阵及其运算-矩阵创建(1)
Codeforces 814 C. An impassioned circulation of affection (dp)
Redis (six) - transaction and lock mechanism of Redis6 (unfinished, to be supplemented)
2023版揽胜运动曝光,安全、舒适一个不落
runtime-core.esm-bundler.js?d2dd:218 Uncaught TypeError: formRef.value?.validate is not a function
leetcode:334. 递增的三元子序列
从产品维度来看 我们为什么不能完全信任Layer2?
Taro小程序跨端开发入门实战
Mobile and PC compatible loading and toast message plugins
Programmers pursue technology to consolidate basic learning route suggestions
Automated Testing and Selenium
干货!ASSANet:让PointNet++更快更强
LeetCode_443_压缩字符串
金九银十跳槽旺季:阿里、百度、京东、美团等技术面试题及答案
GPU加速Pinterest推荐模型,参数量增加100倍,用户活跃度提高16%