当前位置:网站首页>力扣19. 删除链表的倒数第 N 个结点
力扣19. 删除链表的倒数第 N 个结点
2022-04-22 05:32:00 【小何┌】
目录
题目描述

示例:
输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5]
输入:head = [1], n = 1 输出:[]
输入:head = [1,2], n = 1 输出:[1]
题解1
遍历链表,计算链表结点个数count,要删除倒数第k个节点,只需要两个指针分别移动到目标,就可以执行删除操作。

很简单的思路,就不用代码演示了
题解二:快慢指针
思路跟 "返回链表倒数第k个节点" 差不多。
有兴趣可以看一看:力扣.返回倒数第k个节点
定义两个指针p1、p2指向头节点,共N个节点想找倒数第k个。
1 :p2先走k步,停下,此时p2到达正数第(k + 1)个,剩余N - (k + 1)个节点,
2:p1 和 此时的p2一起走,p2到达尾,p1到达正数第(N - k + 1)个,即倒数第k个
3:返回此时的p1
不一样的是,返回指定节点的循环终止条件为:fast != null,而我们要删除节点,所以要找到指定节点的前一个节点,所以终止条件为:fast.next != null
class Solution {
int count = 0;
public ListNode removeNthFromEnd(ListNode head, int k) {
if (head == null) {
return head;
}
ListNode p2 = head;
ListNode p1 = head;
for (int i = 0; i < k; i ++) {
p2 = p2.next;
}
// 如果p2==null,说明k和链表长度相同,此时要删除的就是头节点
if (p2 == null) return head.next;
while (p2.next != null) {
p2 = p2.next;
p1 = p1.next;
}
// 执行删除操作
p1.next = p1.next.next;
return head;
}
}



特殊情况:要删除的是头节点,即 k == count
这种情况下,在第一步,p2会直接运动到null,所以判断一下 p2是不是为空,为空就返回head.next就可以了。
题解三:递归
思路跟这个很像:
class Solution {
// int count = 0;
public ListNode getKthFromEnd(ListNode head, int k) {
// 如果遇到尾节点,终止递归
if (head == null) {
return head;
}
// 一直往里面递,(p一直都是null)
ListNode p = getKthFromEnd(head.next,k);
if (++count == k) {//如果到达目标节点,则返回它的下一个节点
return head.next;
}else {
return p; // 没找到就继续往回归
}
}
}
版权声明
本文为[小何┌]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_62939743/article/details/124137117
边栏推荐
- Unity list uses find or findall to find specific elements and the number of specific elements
- C語言練習(2)——鏈錶實現多項式加法
- Auto.js 画布设置防锯齿paint.setAntiAlias(true);
- GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.2 (1)
- Kaggle_ Detailed explanation of NBME NLP competition baseline (2)
- C language version: traversal mode and reverse order of binary tree
- Neural network -- BP neural network
- MySQL 第6章 Navicat 的安装与使用
- [network protocol] why learn network protocol
- Multithreaded rendering mechanism of Unreal Engine
猜你喜欢

Fundamentals of graphics - depth buffer

Pyqt5 + yolov5 + MSS to realize a real-time desktop detection software
![[WPF] data template selector](/img/6c/ae815d0399a9ae93edd815539b44e1.png)
[WPF] data template selector

Neural network -- basic idea

IT配电及防火限流式保护器应用及选型

The unreal engine uses loadclass to load the blueprint class

Unable to resolve dependency for ': app@debug /compileClasspath': Could not download mapsforge-map. jar

Apache poi HSSF operation Excel

MySQL JDBC 编程
![[nanny installation tutorial] download MySQL 5 from the source code of Linux operating system seven](/img/1a/f2b3da4aa6df9deccf2d1376744b7a.png)
[nanny installation tutorial] download MySQL 5 from the source code of Linux operating system seven
随机推荐
[WPF] use ellipse or rectangle to make circular progress bar
C language version: static establishment of binary tree
idea2021. 1. When writing SQL in mapper: unable to resolve column / table
strcpy的实现你知道吗?
基于有限体积法的传热拓扑优化
Write a program to automatically generate the two-color ball number of welfare lottery with MATLAB
2022.4.21-----leetcode.824
How to use U deep boot U disk to clear the system login password
MySQL高级查询
Unity button long press detection
Codeforces Round #783 (Div. 2) ABCD
GBase 8s V8.8 SQL 指南:教程-5.4
Establishment, addition, deletion, modification and query of sequence table
[candelastudio edit CDD] - 2.3 - realize the jump between multiple securitylevels of $27 service (UDS diagnosis)
MySQL事务
Integer source code
C language version: establishment and basic operation of two-way linked list
Implementation of unity simple event system
2022.4.21-----leetcode. eight hundred and twenty-four
Unity first order Bezier curve for throwing objects