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 ...
随机推荐
- Light OJ 1027 - A Dangerous Maze(概率)
题目大意: 你在一个迷宫里,你面前有n个门,你选择门的概率是一样的,每扇门有一个数字k, 加入这个数字是负数,那么这个门会花费你abs(k)分钟后把你带回原点, 假如这个数字是正数,他可以把你带出迷宫 ...
- 【转】Any way to implement BLE notifications in Android-L preview----不错
原文网址:http://stackoverflow.com/questions/24865120/any-way-to-implement-ble-notifications-in-android-l ...
- sql server 删除索引的语句
DROP INDEX index_name ON talbe_nameDROP INDEX IX_TBlueyBook_10 ON 表名
- Android 子线程请求ASP.NET后台
首先定义布局文件,及点击事件 public class MainActivity extends Activity { private final int MSG_HELLO = 0; private ...
- 安装ucenter 步骤详解及supesite 安装详解
最近弄一个 php 的cms ,花了周六日时间研究了一下,这里记录一下,首先在网页上下载ucenter(分为 gbk 或者utf8版本) 首先下载ucenter 之后,解压之后,upload 里的 ...
- Combination Sum II —— LeetCode
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- prim模板题
题目链接:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1223 #include <cstdio> #include < ...
- python 几种常见的测试框架
1. unittest 参考文档: https://docs.python.org/3/library/unittest.html The unittest unit testing framewor ...
- 3Dmax导出fbx文件缺失纹理问题
- PhoneGap应用开发的那些坑爹事儿
子曾经曰过:如果你恨一个人,让他去开发PhoneGap应用:如果你爱一个人,让他去开发PhoneGap应用. 去年这个时候我很烦恼,因为我觉得我OUT了. 起因是我买了一台Android系统的手机.当 ...