Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点
【转载请注明】http://www.cnblogs.com/igoslly/p/8672656.html
看一下题目:
|
Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note: |
题目大意: 给定一个链表,删除这个链表倒数第n个元素 注意: 1、n值有效 2、尝试一遍结束 |
思路1:
1、先计算链表的总长度,遍历一次
2、得到总长度 length/cnt,计算出倒数第n个元素的位置,再遍历到该位置
实现方法1:
由于题目涉及到 [1],n=1的情况,直接返回NULL,如果依旧使用 ListNode * p = head 的遍历,在语句 p->next=p->next->next; 需要额外添加判断语句,否则会报错;
我们这里额外定义了无意义的 ListNode *res 结果结点,next 指向head,巧妙地避免上类情况,结果 return res->next。
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *res=new ListNode();
res->next=head;
ListNode *p=head;
int cnt=;
// 链表计数
while(p){
cnt++;
p=p->next;
}
p=res;
// 位置从1-cnt,倒数第n个数,为cnt-n+1
for(int i=;i<cnt-n+1;i++){
p=p->next;
}
p->next=p->next->next;
return res->next;
}
};
思路2:
当然题目要求,这道题自然有遍历一遍的方法
要恰好地得到倒数第n个结点位置,那么需要增加临时指针 *p1 , *p2,使两者保持n的距离
那么当 p1 达到末尾时,p2 即指向被删除结点
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode *res=new ListNode();
res->next=head;
ListNode *p=res;
// head 做先指针,先走n布
while(n--){head=head->next;}
// head和p保持n步距离,直到head到末尾NULL
while(head!=NULL){
head=head->next;
p=p->next;
}
// 删除结点
p->next=p->next->next;
return res->next;
}
};
Leetcode0019--Remove Nth Node From End of List 移除链表第N个结点的更多相关文章
- [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 ...
- 【LeetCode每天一题】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: ...
- 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 ...
- [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 ...
- LeetCode第[19]题(Java):Remove Nth Node From End of List(删除链表的倒数第N个节点)
题目:删除链表的倒数第N个节点 难度:Medium 题目内容: Given a linked list, remove the n-th node from the end of list and r ...
- 25.Remove Nth Node From End of List(删除链表的倒数第n个节点)
Level: Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its ...
- 19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点
题目 Given a linked list, remove the n-th node from the end of list and return its head. *Example: Giv ...
- 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现
题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...
- [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 ...
随机推荐
- ACM蓝桥杯之换硬币问题
题目描述: 想兑换100元零钱,有1元.2元.5元.10元四种面值,总有多少种兑换方法? 解题思路: 本题可以采用多种方法求解.最容易理解的应该就是暴力穷举和递归求解.那么本文主要介绍这两种解法. 暴 ...
- 如何在redhat 7上安装VNC服务器
平时我们基本上都是用xshell或者用putty远程我们的linux服务器,如果我们的linux服务器安装了图型化界面那我们又该如何远程使用我们的图形化界面呢?下面我们用vnc来实现远程我们的linu ...
- Switch组件
Switch组件,业务需求中经常遇到.我司的旧项目中,由于没有使用较为成熟点的组件库.自己实现了一个switch组件,但是实现的略微有些丑陋. 实现基本需求 https://jsfiddle.net/ ...
- vue数据绑定源码
思路分析 数据的双向绑定,就是数据变化了自动更新视图,视图变化了自动更新数据,实际上视图变化更新数据只要通过事件监听就可以实现了,并不是数据双向绑定的关键点.关键还是数据变化了驱动视图自动更新. 所有 ...
- Python网络请求urllib和urllib3详解
Python网络请求urllib和urllib3详解 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urlli ...
- poj 2553强连通+缩点
/*先吐槽下,刚开始没看懂题,以为只能是一个连通图0T0 题意:给你一个有向图,求G图中从v可达的所有点w,也都可以达到v,这样的v称为sink.求这样的v. 解;求强连通+缩点.求所有出度为0的点即 ...
- PatentTips - Invalidating TLB entries in a virtual machine system
BACKGROUND This invention relates to virtual machines. In particular, the invention relates to trans ...
- JVM内存管理和垃圾回收机制介绍
http://backend.blog.163.com/blog/static/20229412620128233285220/ 内存管理和垃圾回收机制是JVM最核心的两个组成部分,对其内部实 ...
- HDU 4542
T_T终于让我过了,坑啊,竟然时限是200ms... 我是预处理出不整除了个数的,因为这个较容易一点.利用算术基本定理,f=p1^a1*p2^a2...... 所以,整除它的个数就是(a1+1)*(a ...
- BZOJ 1492 货币兑换 cdq分治或平衡树维护凸包
题意:链接 方法:cdq分治或平衡树维护凸包 解析: 这道题我拒绝写平衡树的题解,我仅仅想说splay不要写挂,insert边界条件不要忘.del点的时候不要脑抽d错.有想写平衡树的去看140142或 ...