题目链接

  题目要求:

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

  For example,

   Given linked list: ->->->->, and n = .

   After removing the second node from the end, the linked list becomes ->->->.

  Note:

  Given n will always be valid.
  Try to do this in one pass.

  这道题比较常规的想法是先算出链表长度len,再将头指针移动len-n步就可以到达待删节点了。具体程序如下(4ms):

 /**
* 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)
return head; int len = ;
ListNode *start = head;
while(start)
{
len++;
start = start->next;
} if(len == n)
{
start = head;
head = head->next;
delete start;
start = nullptr;
return head;
} ListNode *preNode = head;
start = preNode->next;
int k = ;
while(k < len - n)
{
preNode = start;
start = start->next;
k++;
} preNode->next = start->next;
delete start;
start = nullptr; return head;
}
};

  另一个比较巧妙的方法就是先定义一个慢指针(初始值为头指针父指针),让头指针移动n步,再让头指针和一个慢指针同时移动直至头指针为空,这时慢指针指向的下一个节点就是待删节点。具体程序日如下(4ms):

 /**
* 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)
return head; ListNode *dummy = new(nothrow) ListNode(INT_MIN);
assert(dummy);
dummy->next = head; for(int i = ; i < n; i++)
head = head->next; ListNode *preNode = dummy;
while(head)
{
head = head->next;
preNode = preNode->next;
} ListNode *delNode = preNode->next;
preNode->next = delNode->next;
delete delNode;
delNode = nullptr; head = dummy->next;
delete dummy;
dummy = nullptr; return head;
}
};

LeetCode之“链表”:Remove Nth Node From End of List的更多相关文章

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

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

  2. 【LeetCode】19. Remove Nth Node From End of List (2 solutions)

    Remove Nth Node From End of List Given a linked list, remove the nth node from the end of list and r ...

  3. 【LeetCode】19. Remove Nth Node From End of List 删除链表的倒数第 N 个结点

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:链表, 删除节点,双指针,题解,leetcode, 力扣 ...

  4. 【一天一道LeetCode】#19. Remove Nth Node From End of List

    一天一道LeetCode系列 (一)题目 Given a linked list, remove the nth node from the end of list and return its he ...

  5. LeetCode OJ 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 ...

  6. LeetCode题解(19)--Remove Nth Node From End of List

    https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 原题: Given a linked list, remove the  ...

  7. 【LeetCode OJ】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: G ...

  8. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  9. 【LeetCode】019. 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 ...

  10. LeetCode:19. Remove Nth Node From End of List(Medium)

    1. 原题链接 https://leetcode.com/problems/remove-nth-node-from-end-of-list/description/ 2. 题目要求 给出一个链表,请 ...

随机推荐

  1. 字符编码(ASCII、ANSI、GB2312、UTF-8等)系统梳理

    引言 在显示器上看见的文字.图片等信息在电脑里面其实并不是我们看见的样子,即使你知道所有信息都存储在硬盘里,把它拆开也看不见里面有任何东西,只有些盘片.假设,你用显微镜把盘片放大,会看见盘片表面凹凸不 ...

  2. 【Unity Shaders】ShadowGun系列之一——飞机坠毁的浓烟效果

    写在前面 最近一直在思考下面的学习该怎么进行,当然自己有在一边做项目一边学OpenGL,偶尔翻翻论文之类的.但是,写shader是一个需要实战和动手经验的过程,而模仿是前期学习的必经之路.很多人都会问 ...

  3. Xcode中为何要为设置bundle和App分别设置两份一样的图片资源

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在App设置的bundle中有时也会用到图片资源,而在 ...

  4. Android 实现图片加水印

    加水印代码 public Bitmap addWaterMark(Bitmap src, String water, Context context){ Bitmap tarBitmap = src. ...

  5. System.getProperty()的用途

     偶尔用到 System.getProperty(),找起来也不方便.这里做下记录备忘: 编写的测试类: public class TestSystemproperty { public stat ...

  6. Java并发框架——公平性

    所谓公平性指所有线程对临界资源申请访问权限的成功率都一样,不会让某些线程拥有优先权.通过前面的CLH Node FIFO学习知道了等待队列是一个先进先出的队列,那么是否就可以说每条线程获取锁时就是公平 ...

  7. 运用 三种 原生 谷歌 阿里 解析和生成json

    三种类生成JSON数据方法 JSON(原生): 第一种 JSONStringer和JSONObject区别在于添加对象时是按顺序添加的比如说 JSONStringer 添加 a:1 b:2 c:3那么 ...

  8. struts extjs 3.3.1 读取JSON文件

    json文件和脚本代码: jsonSrc/jsonTxt1.json, { "personInfoList": [ { "id": 0, "name& ...

  9. Linux内核2.6的进程调度

         Linux是多任务抢占操作系统,多任务就是指多个进程间通过分时切换来并发执行.非抢占的系统是对每个进程而言,除非时间片用完或主动放弃否则不会被剥夺CPU,主动放弃包括调用一些调度的系统调用( ...

  10. (八十)MapKit放置系统默认大头针和自定义大头针

    有关MapView的显示和定位在上一节已经说明,这一节说明如何在地图上放置大头针,以及设置点击大头针的视图. [系统默认大头针] mapView上放置大头针的方法是调用其addAnnotation:方 ...