题目

Given a linked list, remove the n-th node from the end of list and return its head.

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:

Given n will always be valid.

Follow up:

Could you do this in one pass?


思路

思路一:Two Pass

首先定义一个辅助节点auxList,使它的下一个节点指向链表head,用来简化边界判断的条件,例如一个链表只有一个节点,删除了头节点。第一次遍历(one pass),得到链表的长度L;第二次遍历(two pass)设置一个指针指向辅助节点auxList,并开始删除节点,一直到指定的n停止,此时到达第\(L-n\)个节点;将第\(L-n\)与第\(L-n+2\)个节点重新连接,于是就删除了第\(L-n+1\)个节点(因为人为增加一个节点)。

思路二:One Pass

题目让我们思考,如何通过一次循环来实现对链表倒数N个节点的删除。

  • 双指针法

    可以利用双指针法来解决倒数的问题:

    • 第一个指针快一点,用来控制循环的次数
    • 第二个指针慢一点,用于遍历

      两个指针都是指向链表,也就是两个指针对应两个一样的链表。要想一次遍历实现对第\(L-n\)个节点的删除,首先用第一个指针“跑”完n次,此时第二个指针开始“跑”,并且以第一个指针从第n个节点跑到第L-1个节点为循环次数,此时相当于第二个链表跑了\(L-n\)个数,这样就找到了链表中倒数第n个数。
  • 递归

    这种涉及单链表的插入与删除都可以考虑递归,因为插入和删除都要涉及到上一层节点的操作。将最后一个节点设置为第一层,逐层f返回,当返回到第n+1层开始删除。

Tips

单链表

![](https://i.loli.net/2019/05/20/5ce25ec0d432a91619.jpg)
单链表示意图,表头为空,表头的后继节点是"1"
**链表的搜索**
查找链表L中第一个关键字为k的元素,并返回指向该元素的指针。如果链表中没有关键字为k的对象,则返回空(NIL)
```cpp
//List-Search(L,k)
x = L.head;
while x != NIL and x.key != k //NIL为空
x = x.next
return x
```
**链表的插入**
![](https://i.loli.net/2019/05/20/5ce2611b14b8675624.jpg)
图:链表的插入

链表的删除

![](https://i.loli.net/2019/05/20/5ce26180b2ff342106.jpg)
图:链表的删除
---
#C++
* 思路一
```cpp
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* auxList = new ListNode(0);
auxList->next = head;
int length = 0;
ListNode* pass = head;
while(pass != NULL){
length ++;
pass = pass->next;
}

    length -= n;
pass = auxList;
while(length > 0){
length --;
pass = pass->next;
} pass->next = pass->next->next; return auxList->next; //这里返回next是因为auxList的next才是原来的链表
}
* 思路二(1)
```cpp
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* auxList = new ListNode(0);
auxList->next = head;
ListNode* point1 = auxList;
ListNode* point2 = auxList; for(int i = 1; i <= n + 1;i++)
point1 = point1->next; while(point1 != NULL){
point1 = point1->next;
point2 = point2->next;
} point2->next = point2->next->next; return auxList->next;
}
  • 思路二(2)
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* auxList = new ListNode(0);
auxList->next = head;
remove(auxList, n);
return auxList->next;
} int remove(ListNode* head, int n){
if(head->next == NULL)
return 1;
int levels = remove(head->next, n) + 1;
if(levels == n+1)
head->next = head->next->next;
return levels;
}

Python


参考

[1] 算法导论第10章10.2,p131

[2] http://www.cnblogs.com/skywang12345/p/3561803.html

19. Remove Nth Node From End of List[M]删除链表的倒数第N个节点的更多相关文章

  1. 19. Remove Nth Node From End of List C++删除链表的倒数第N个节点

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 使用双指针法,可以仅遍历一次完成节点的定位 /** * Definiti ...

  2. LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

    题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->n ...

  3. 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 ...

  4. 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 ...

  5. 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现

    题目描述: 中文: 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点. 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二 ...

  6. [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 ...

  7. [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 ...

  8. 61. Rotate List(M);19. Remove Nth Node From End of List(M)

    61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. ...

  9. 《LeetBook》leetcode题解(19):Remove Nth Node From End of List[E]——双指针解决链表倒数问题

    我现在在做一个叫<leetbook>的开源书项目,把解题思路都同步更新到github上了,需要的同学可以去看看 这个是书的地址: https://hk029.gitbooks.io/lee ...

随机推荐

  1. RAP、Mock.js、Vue.js、Webpack

    最近做项目使用的是RAP1的接口,但是昨天开始,RAP1 出现了问题,接口都不能用了. 所以补充一下Mock.js的用法,以便在这种突发的情况时候时自己通过Mock的方式来处理接口. npm init ...

  2. Git Learning Part I - Install Git and configure it

    Why we need 'Git' GIt version control: 1. record the history about updating code and deleting code 2 ...

  3. BeautifulSoup 库的使用记录

    BeautifulSoup 有何用途 如果我们需要通过脚本来抓取网络中的数据时,使用传统的字符解析等方法时是非常低效的,而BeautifulSoup则可以方便的通过接口来获取标签中所想要得到的数据.主 ...

  4. 机器学习PAI为你自动写歌词,妈妈再也不用担心我的freestyle了(提供数据、代码)

    背景 最近互联网上出现一个热词就是“freestyle”,源于一个比拼rap的综艺节目.在节目中需要大量考验选手的freestyle能力,freestyle指的是rapper即兴的根据一段主题讲一串r ...

  5. hdu3416 Marriage Match IV 最短路+ 最大流

    此题的大意:给定一幅有向图,求起点到终点(都是固定的)的不同的最短路有多少条.不同的最短路是说不能有相同的边,顶点可以重复.并且图含有平行边. 看了题以后,就想到暴力,但是暴力往往是不可取的.(暴力的 ...

  6. 2014 Container技术大会:未来Linux Container会是PaaS平台的核心

    不应错过2014 Container技术大会的九大理由. 一.Docker官方人员再次来到北京,首次向中国布道Docker技术.2013年Docker高级软件工程师Jerome Petazzoni,曾 ...

  7. 逐步理解Java中的线程安全问题

    什么是Java的线程安全问题? 线程安全就是多线程访问时,采用了加锁机制,当一个线程访问该类的某个数据时,进行保护,其他线程不能进行访问直到该线程读/写完,其他线程才可使用.不会出现数据不一致或者数据 ...

  8. windows端口被占用解决办法

    1.查找端口 netstat -ano | findstr 端口号 2.进程列表并查找相应的进程 tasklist |findstr 进程号 3.杀死进程 taskkill /f /t /im 进程名 ...

  9. jQuery中cookie使用方法

    <script type="text/javascript"> $(function () { var content=$.cookie('text');  //得到c ...

  10. 【hdu 6444】Neko's loop

    [链接] 我是链接,点我呀:) [题意] 给你一个序列. 你可以选择起点i. 然后每次往右跳k次. 得到下一个值a[i+k];. 问你跳m次能得到的最大值ma是多少. 如果>=s输出0 否则输出 ...