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 ...
随机推荐
- scp命令的使用
scp命令是什么 scp是 secure copy的缩写, scp是linux系统下基于ssh登陆进行安全的远程文件拷贝命令. scp命令用法 scp [-1246BCpqrv] [-c cipher ...
- javascript对象(3)
这个对象,不是那个对象,第三哦! 对象之间会存在继承,所以,来说一下他们之间存在的三种三种继承方式: 1.冒用继承 //创建了孙悟空构造函数 function Sun(change,weapon,gf ...
- Filter学习总结,顺便提及点servlet3.0异步filter和异步监听
Filter介绍: Filter在项目中经常可以用到,通常配置在web.xml中.是服务器端的一个组件,对于用户的请求和响应数据进行过滤操作,控制是否让用户访问到对应的web资源.常用于编 ...
- day005-异常
1. 异常概念 1.1 异常的继承体系 异常的根类:java.lang.Throwable,其下有两个子类: Java.lang.Error Java.util.Exception ...
- 【Leetcode】【Easy】Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 手写vector
看过JDK源码,现在自己想实现一个vector. 最开始的时候,我大概构想了一下怎么设计,一种是设置一个指针数组来存放对象,这样修改的时候可以不用大量的元素复制,但后来仔细想了想,它需要设置一个额外的 ...
- WAKE-WIN10-SOFT-VS2013及工具
1,下载安装,,,,,,, 2,配置 3opencv 3,1官网:http://opencv.org/ 3,3VS2013+OPENCV249配置 http://jingyan.baidu.com/a ...
- 谣言粉碎机 - 极短时间内发送两个Odata request,前一个会自动被cancel掉?
背景 有时我们能在Chrome开发者工具的Network tab里观察到SAP UI5应用会发出某些状态为"取消"的OData请求.如下图第五个请求. 之前有一种似是而非的说法:极 ...
- BZOJ3053:The Closest M Points(K-D Teee)
Description The course of Software Design and Development Practice is objectionable. ZLC is facing a ...
- MYSQL5.7.15安装步骤
下载完成之后双击安装: 接下来一路next (出现的问题) 在我第一次安装myslq过程中,上图中的mysql server failed ,这是因为电脑环境需要升级一个插件,Visual C++ 2 ...