当前位置:网站首页>19. 删除链表的倒数第 N 个结点
19. 删除链表的倒数第 N 个结点
2022-07-28 09:53:00 【vv1025】
https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/
19. 删除链表的倒数第 N 个结点
难度中等1625
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
进阶:你能尝试使用一趟扫描实现吗?
示例 1:

输入:head = [1,2,3,4,5], n = 2 输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1 输出:[]
示例 3:
输入:head = [1,2], n = 1 输出:[1]
提示:
- 链表中结点的数目为
sz 1 <= sz <= 300 <= Node.val <= 1001 <= n <= sz
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
}
};#include <iostream>
#include <cassert>
using namespace std;
///Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// LinkedList Test Helper Functions
ListNode* createLinkedList(int arr[], int n){
if(n == 0)
return NULL;
ListNode* head = new ListNode(arr[0]);
ListNode* curNode = head;
for(int i = 1 ; i < n ; i ++){
curNode->next = new ListNode(arr[i]);
curNode = curNode->next;
}
return head;
}
void printLinkedList(ListNode* head){
if(head == NULL){
cout<<"NULL"<<endl;
return;
}
ListNode* curNode = head;
while(curNode != NULL){
cout << curNode->val;
if(curNode->next != NULL)
cout << " -> ";
curNode = curNode->next;
}
cout << endl;
return;
}
void deleteLinkedList(ListNode* head){
ListNode* curNode = head;
while(curNode != NULL){
ListNode* delNode = curNode;
curNode = curNode->next;
delete delNode;
}
return;
}
/// Get the total length and remove the nth node
/// Two Pass Algorithm
///
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
int length = 0;
for(ListNode* cur = dummyHead->next ; cur != NULL ; cur = cur->next)
length ++;
int k = length - n;
assert(k >= 0);
ListNode* cur = dummyHead;
for(int i = 0 ; i < k ; i ++)
cur = cur->next;
ListNode* delNode = cur->next;
cur->next = delNode->next;
delete delNode;
ListNode* retNode = dummyHead->next;
delete dummyHead;
return retNode;
}
};
int main() {
int arr[] = {1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(int);
ListNode* head = createLinkedList(arr, n);
printLinkedList(head);
head = Solution().removeNthFromEnd(head, 2);
printLinkedList(head);
deleteLinkedList(head);
return 0;
}边栏推荐
- [jzof] 15 bits of 1 in binary
- Plato Farm-以柏拉图为目标的农场元宇宙游戏
- Read Plato farm's eplato and the reason for its high premium
- Description of landingsite electronic label quppa firmware entering DFU status
- 今天和大家聊一聊mysql数据库的数据类型
- Pycharm uses CONDA to call the remote server
- Edge团队详解如何通过磁盘缓存压缩技术提升综合性能体验
- [ESP32][esp-idf] esp32s3快速搭建LVGLV7.9
- Database advanced learning notes -- storage functions
- 选择供应商服务系统,是大健康产业企业迈向数字化转型的第一步
猜你喜欢

我用小程序容器让移动研发效率提升了5倍!

Joint search set

【JZOF】14剪绳子

高温天气筑牢安全生产防线,广州海珠区开展加油站应急演练

office2013以上输入数学公式

OSPF expansion configuration, routing principles, anti ring and re release

Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)

OSPF的拓展配置,选路原则,防环及重发布
![[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9](/img/39/8efef047d0a9223b97819a54b5edf8.png)
[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9

2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了
随机推荐
[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9
Include and require include_ Once and require_ Once difference
Installing MySQL for Linux operating system (centos7)
Flink - checkpoint Failure reason: Not all required tasks are currently running
PHP连接mysql原生代码
13 probability distributions that must be understood in deep learning
能够遍历一个文件夹下的所有文件和子文件夹
Fixedwindowrollingpolicy introduction
判断字符串是不是回文
Flink - checkpoint Failure reason: Not all required tasks are currently running
PHP 常用的数组整理
Data can't lie. Plato farm is the leader of the meta universe
fastjson中@jsonType注解的功能简介说明
OSPF的LSA及优化
2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了
2022-7-27周报
超级原始人系列盲盒即将上线,PlatoFarm赋能超多权益
Software testing and quality learning notes 2 - black box testing
Time series analysis 41 - time series prediction tbats model
Function introduction and description of @jsontype annotation in fastjson