LeetCode 删除链表倒数第N个节点
基本思路
- 定义两个指示指针a b
- 让a先行移动n+1个位置
- 若a指向了NULL的位置,则删除的是头节点(由于走过了n+1个节点刚好指在尾部的NULL上)
- 否则让b与a一起移动直至a->next,即a的下一个节点为NULL,则此时b的下一个节点为要删除的节点
- 删除下一个节点
代码实现
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* a = head;
ListNode *b =head;
int c = 1;
//令a指向第n+1个节点
while(c!=n+1){
a = a->next;
c++;
}
if(a == NULL){
ListNode * t = head;
head = head->next;
delete t;
return head;
}
//a b 同时向前走
while(a->next != NULL){
a = a->next;
b = b->next;
}
ListNode * t = b->next;
b->next = b->next->next;
delete t;
return head;
}
};
LeetCode 删除链表倒数第N个节点的更多相关文章
- Leetcode算法系列(链表)之删除链表倒数第N个节点
Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...
- [leetcode]19. Remove Nth Node From End of List删除链表倒数第N个节点
Given a linked list, remove the n-th node from the end of list and return its head. Example: Given l ...
- 删除链表倒数第n个节点
题目: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链 ...
- 19。删除链表倒数第N个节点
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next# 这道题还是很简单的,我们只 ...
- [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点
Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...
- [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- 删除单链表倒数第n个节点
基本问题 如何删除单链表中的倒数第n个节点? 常规解法 先遍历一遍单链表,计算出单链表的长度,然后,从单链表头部删除指定的节点. 代码实现 /** * * Description: 删除单链表倒数第n ...
- leetcode 19.删除链表的第n个节点
删除链表的第n个节点 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第 ...
- [LeetCode] 19. Remove Nth Node From End of List 移除链表倒数第N个节点
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
随机推荐
- CRM——起步
一.CRM简介 crm 客户关系管理软件 ( Customer Relationship Management ). 二.CRM起步 1.设计表结构和数据库迁移 from django.db impo ...
- Spring MVC 参数必填项导致客户端报 HTTP 400 并且无法进入断点的问题
1.问题 Spring MVC 在参数上设置了必填项,post 请求时报 HTTP 400 并且未进入断点,如将“年龄”设置为了必填项: @RequestParam( value="age& ...
- PLSQL Developer乱码
1.select * from v$nls_parameters 查询nls的参数,获得数据库服务器端的字符编码 NLS_LANGUAGE NLS_CHARACTERSET 2.修改本地环境变量,设置 ...
- 慧都启动“正版IDE联合超值推广计划
越来越多的中国软件企业为盗版所害而蒙受巨大损失,正版化意识逐渐兴起.IDE(集成开发环境)是软件开发.编写代码必备工具,而正版IDE更具有运行更稳定.编码更安全.保障更加完善等特点,逾为中国软件行业企 ...
- SharePoint 2013 - Add-ins
1. App Web & Host Web The special website to which the app is deployed is called an App Web. The ...
- Arcgis flex 切片地图麻点
在arcgis server中发布地图切片完成后,有时候在访问地图的时候会出现很多麻点, 其实是你切片的时候没有注意到一些选项.... 默认的切片是PNG8,说到这可能就明白了吧,png8的色彩范围: ...
- Android SDK 目录和作用详解
1.add-ons这里面保存着附加库,比如GoogleMaps,当然你如果安装了OphoneSDK,这里也会有一些类库在里面.也可以是厂商自己制作的add-ons.开发中该文件是可选的,如果你没有用到 ...
- Servlet和SpringMVC补课
1.web.xml加载顺序 http://mianhuaman.iteye.com/blog/1105522 关键点:ServletContext -> context-param -> ...
- 自动驾驶self driving知识点mark
C++, algorithm, RTOS,TX2, CAN, 标准, car model,
- DeepQA websocket 并发测试
var client = new Array(); var W3CWebSocket = new Array(); var concurrent = 2; for (var i = 0; i < ...