当前位置:网站首页>19. 删除链表的倒数第 N 个结点
19. 删除链表的倒数第 N 个结点
2022-07-21 05:15:00 【安吉_lh1029】
1、题目描述
2、题目分析
根据题目的几个示例,存在以下几种特殊情况:
【双指针思路】:
(1)第一个指针node先走 n 步;
(2)获取目标删除节点的【前一个】节点, 此时head的前一个节点pre ,跟第一个指针node, 相差 (n+1)个位置 ;
(3) 接下来两个指针,pre, node分别进行遍历,不难知道, 当node为null 时,pre刚好是目标删除节点的前一个节点;
(4)删除pre后的节点【既目标删除节点】pre.next = pre.next.next;
基础内容,链表的结构体:
class ListNode{
int val;
ListNode next;
ListNode(){}
ListNode(int val){
this.val = val;
}
ListNode(int val, ListNode next){
this.val = val;
this.next = next;
}
}
实现代码如下:
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummy = new ListNode(0,head);
ListNode node = head;
ListNode pre = dummy;
//第一次,节点node先走 n 步
for(int i = 0; i < n; i++){
if(node != null)
node = node.next;
else
return null;
}
//第二次,pre和node均继续走,此时pre和node相差(n+1)个位置,当node为null时,pre刚好是目标删除节点的前一个节点
while(node != null){
pre = pre.next;
node = node.next;
}
//删除pre后面的元素
pre.next = pre.next.next;
return dummy.next;
}
}
复杂度分析
时间复杂度:O(L),其中 L 是链表的长度。
空间复杂度:O(1), 无使用额外空间。
边栏推荐
- 10 classic C language interview basic algorithms
- 庖丁解牛斐波拉契数列和背包问题——详细解析两个问题优化过程,带你从最基本的问题看懂动态规划!!!
- Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
- Appium+pytest+allure realizes app automated testing, which is a small test
- 堆-原理到应用——堆排序、优先级队列
- Google Chrome -- xpathhelper installation
- Datart data visualization works are open source | chart plug-in works are all open source, which can be extracted through Baidu cloud download link
- Using tornado to realize local chat room
- 概率论-大数定律和中心极限定理
- Yolov4 (Darknet version) post processing: display confidence and save the contents of the detection box locally
猜你喜欢
Source insight 4.0 personalization and shortcut keys
Heap - principle to application - heap sorting, priority queue
Blockbuster: the domestic ide was released and developed by Alibaba. It is completely open source (high performance + high customization)
Interview question: what is the difference between clustered index and non clustered index?
轻松自制PASCAL VOC数据集
datart 自定义插件,不改动源代码,让 BI 顺利完成又一次创新
LinkedList源码深度剖析
Do you still have certificates to participate in the open source community?
Scratch graphical programming operation hardware
前 3 名突然变了?揭秘 7 月编程语言最新排行榜
随机推荐
01-自动化测试基础--(selenium八股文部分+环境配置+八大定位)
竟然有人把VSCode玩成了IDEA的效果,有点厉害
Special answer: what does NMN concept stock mean and what is NMN in the end
一起学习多线程、进程、开发板
堆-原理到应用——堆排序、优先级队列
05 unittest extension
Basic process of APP testing and key points of APP testing
堪比“神仙打架”的开源数据可视化社群,你见过吗?
02-selenium的进一步学习(控制浏览器窗口+)
一起学习gcc gdb makefile
Linear regression finale (ridge, Lasso regression principle, formula derivation), you want everything here
In depth analysis of ArrayList source code, from the most basic capacity expansion principle, to the magic iterator and fast fail mechanism, you have everything you want!!!
线上 JVM 内存问题定位
datart 开源数据可视化应用 | 手把手教你开发出优秀的图表插件
Teach you how to use yolov4 training and testing data set on the server (nanny level)
openGl新手入门学习笔记(一)什么是openGl,使用glfw库和环境搭建
Interview question: what is the difference between clustered index and non clustered index?
VS2019+OpenCV安装与配置教程
Summary of Niuke online question brushing -- top101 must be brushed for interview
JS advanced road - continuous update