当前位置:网站首页>力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
2022-08-09 01:51:00 【@白圭】
一、题目
二、代码
/** * 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) {
///统一链表格式
ListNode* dummyhead = new ListNode(0); //设置虚假的头结点 为了使得单节点和多节点情况统一
dummyhead->next = head;
ListNode* process_node = new ListNode(0);
process_node=dummyhead;
int the_sum_of_the_node=0;
while(head!=nullptr)
{
the_sum_of_the_node+=1;
head=head->next;
}
std::cout<<"the_sum_of_the_node "<< the_sum_of_the_node <<std::endl;
int the_number_of_the_delete=the_sum_of_the_node+1-n;
std::cout<<"the_number_of_the_delete "<< the_number_of_the_delete <<std::endl;
int count_the_number=1;
while(count_the_number<the_number_of_the_delete)
{
count_the_number=count_the_number+1;
process_node=process_node->next;
}
//跳出时,process_node应该正好指向需要删除的节点前一个
process_node->next=process_node->next->next;
return dummyhead->next;
}
};
三、运行结果
边栏推荐
猜你喜欢
随机推荐
clickhouse 思维导图
mysql连接超过八小时报错
TP测试查询数据库字段为null或空的字段
[C language brush questions] Application of fast and slow pointers in linked lists
Codeforces Round #809 (Div. 2)A~D1
OIDC 思维导图
WPF效果第一百九十四篇之伸缩面板
『Another Redis DeskTop Manager』用了这款Redis可视化工具,分析效率提升12倍
【图像增强】基于Step和Polynomial 滤波实现图像增强附matlab代码
软件测试的调用接口怎么调用,逻辑是什么?
Wireshark packet capture tool
德语翻译器在线翻译中文
企业里Foxmail邮箱问题解决方法汇总
33. 分别谈谈联合索引生效和失效的条件
2022 PMP Project Management Certification Exam Registration Guide (1)
OpenSceneGraph3.5.1编译
typescript90-使用类型文件声明类型
Introduction to LVGL (based on v8.1-8.2)
The Best Open Source Web Application Firewall to Protect Your Web Applications
Bugs encountered in remote control projects