当前位置:网站首页>10.1-----19. Delete the Nth node from the bottom of the linked list
10.1-----19. Delete the Nth node from the bottom of the linked list
2022-08-09 02:06:00 【@baigui】
一、题目
二、代码
/** * 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_nodeShould point exactly to the one before the node that needs to be deleted
process_node->next=process_node->next->next;
return dummyhead->next;
}
};
三、运行结果
边栏推荐
- mysql 5.7 入坑
- MT4 / MQ4L entry to the master of EA tutorial lesson two (2) - - MQL language commonly used function account information commonly used functions
- 考研人总结的时间管理7大忌,你中了几条?
- 显著性检验--学习笔记
- 数字孪生+燃气管理,开启智慧燃气管理新模式
- Codeforces Round #809 (Div. 2)A~D1
- How js implements array deduplication (7 kinds)
- Cmake 报错 Could not find a package configuration file provided by “OpenCV“
- LeetCode每日两题02:第一个错误的版本 (均1200道)方法:二分查找
- 年金险的安全性怎么样啊?可靠吗?
猜你喜欢
随机推荐
How SEMRush finds keywords for advertising
Z-Game on grid(牛客多校赛)
程序员的日常生活 | 每日趣闻
HNUMSC-C语言第一课
Wireshark packet capture tool
方法参数
虹科技术|如何阻止供应链攻击?
ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...
Go-11 - Process Control
mysql连接超过八小时报错
Dapr学习(4)之eShopOnDapr部署(Rancher2.63&k3s)
Go-7-RESTful API的设计
右键新建缺少word、excel选项问题处理
Go-12-结构体
webrtc 编译
JDBC技术(一)——一个简单的JDBC测试
Composer usage record
字节输入流(InputStream)与字节输出流(OutputStream)
Latex示例参考
class path resource [bean.xml] cannot be opened because it does not 错误解决方案