当前位置:网站首页>力扣刷题记录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;
}
};
三、运行结果
边栏推荐
- Image denoising based on edge enhancement Diffusion 】 (cEED) and Coherence Enhancing coursing together (cCED) filter to realize image denoising matlab code
- 如何仿造一个websocket请求?
- LVGL简介(基于v8.1-8.2)
- Go-8-Gin framework
- Go-12-Structure
- 等到中心化的平台不再,衍生于这个平台的一切都将化作泡影
- Mysql高级篇(逻辑架构和存储引擎)
- Docker redis master-slave replication setup, the container cannot be started?
- JDBC技术(一)——一个简单的JDBC测试
- Codeforces Round #809 (Div. 2)A~D1
猜你喜欢
随机推荐
LeetCode精选200道--双指针篇
Difference between KQL and Lucene
gstreamer 记录
Latex示例参考
观察者模式
TCP/IP协议栈
数据恢复软件EasyRecovery支持恢复所有类型的文件
2022年中国全民健身发展白皮书
OIDC 思维导图
The Best Open Source Web Application Firewall to Protect Your Web Applications
深度学习模型的两种部署:ONNX与Caffe
String compression
PMP有什么答题技巧?
Go-8-Gin framework
HCIP-R&S By Wakin自用笔记(3)OSPF之各类LSA及LSA更新规则
Qt中QFile、QByteArray QDataStream和QTextStream区别
seaborn 笔记: 绘制分类数据
JDBC technology (1) - a simple JDBC test
Go-11 - Process Control
Data recovery software EasyRecovery supports recovery of all types of files