19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)
题目意思:去掉链表中倒数第n个节点
思路:1.两次遍历,没什么技术含量,第一次遍历计算长度,第二次遍历找到倒数第k个,代码不写了
2.一次遍历,两个指针,用指针间的距离去计算。

ps:特别注意删掉第一个节点的情况,在前面加个头结点,则变为了删除第二个节点的情况
/**
* 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) {
if(head==NULL||n==)return NULL;
ListNode* ans=new ListNode();
ans->next=head; //加了一个头结点
ListNode *p1,*p2;
p1=p2=ans;
for(int i=;i<n;++i){ //p2指向第n+1个节点
p2=p2->next;
}
while(p2->next){ //p2指向最后一个,p1指向倒数第n-1个
p2=p2->next;
p1=p1->next;
}
p1->next=p1->next->next; //删掉倒数第n个节点
return ans->next;
}
};
19 Remove Nth Node From End of List(去掉链表中倒数第n个节点Easy)的更多相关文章
- lintcode:Remove Nth Node From End of Lis 删除链表中倒数第n个节点
题目: 删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 样例 给出链表1->2->3->4->5->null和 n = 2. 删除 ...
- [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 ...
- 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...
- 【LeetCode】Remove Nth Node From End of List(删除链表的倒数第N个节点)
这道题是LeetCode里的第19道题. 题目要求: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, ...
- 019 Remove Nth Node From End of List 删除链表的倒数第N个节点
给定一个链表,删除链表的倒数第 n 个节点并返回头结点.例如,给定一个链表: 1->2->3->4->5, 并且 n = 2.当删除了倒数第二个节点后链表变成了 1->2 ...
- Leetcode19.Remove Nth Node From End of List删除链表的倒数第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 n-th node from the end of list and return its head. Example: Given l ...
- leetcode 19. Remove Nth Node From End of List(链表)
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个结点
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
随机推荐
- Delphi 弹出Windows风格的选择文件夹对话框, 还可以新建文件夹
Delphi 弹出Windows风格的选择文件夹对话框, 还可以新建文件夹 unit Unit2; interface uses Windows, Messages, SysUtils, V ...
- VS插件 热
1. AnkhSVN - Subversion SCC Providerhttp://ankhsvn.open.collab.net/ AnkhSVN是一个VS的Subversion 源代码管理提供者 ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
- [Sequence Alignment Methods] Dynamic time warping (DTW)
本系列介绍几种序列对齐方法,包括Dynamic time warping (DTW),Smith–Waterman algorithm,Cross-recurrence plot Dynamic ti ...
- C++的一些内置函数
C++里面有一些内置函数,实现了一些常用功能.虽然我手写也能写出这些函数,但是在srm或者其他一些需要速度的地方,用内置函数的优势就能体现出来了. 1.__gcd(a, b),返回a,b的最大公约数, ...
- sql给整数补零
update hs_user.clientorder a set a.stockcode = lpad(a.stockcode,6,'0') where a.market = 'SZ'
- Appium移动自动化测试(三)--安装Android模拟器(转)
Appium移动自动化测试(三)--安装Android模拟器 2015-06-08 10:33 by 虫师, 30828 阅读, 9 评论, 收藏, 编辑 当Android SDK安装完成之后,并不意 ...
- 解决python “No module named pip”
python 升级后导致不能使用原来的pip命令 windows平台 cmd中敲命令:python -m ensurepip 得到pip的setuptools 然后就可以用:easy_install ...
- Java学习的随笔(3)接口
首先是一段<Java编程思想>中,对接口的解释:“interface这个关键字产生一个完全抽象的类,它根本就没有提供任何具体的实现.它允许创建者确定方法名.参数列表.返回类型,但是没有任何 ...
- Apache XAMPP Fails to start under Windows XP
Apache XAMPP Fails to start under Windows XP I’ve been installing XAMPP a hundred times before since ...