题目说明

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:

Given n will always be valid.

Try to do this in one pass.


题目分析

看上去还挺简单的,由于题目要求只遍历一遍链表,我的做法是采用两个指针,保证两个指针的距离为n,这样当前面的指针到达尾部的时候,另一个指针的位置就是要移除的结点。

以下为个人实现(C++,糟糕的一坨代码):

/**
* 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) {
ListNode *new_head = new ListNode(-1);
new_head->next = head;
ListNode *p1 = new_head, *p2 = new_head;
for (int i = 0; i <= n; i++, p2 = p2->next); // p2 = p1 + n + 1
for (; p2 != NULL; p2 = p2->next, p1 = p1->next);
p2 = p1->next;
p1->next = p2->next;
delete(p2);
p2 = new_head->next;
delete(new_head);
return p2;
}
};

这里比较僵硬的一点是链表头的元素需要额外进行判断,我这种算法可以说很辣鸡了……

于是在讨论版看到了另外一种和我思路一样,但是细节不同的代码实现:

class Solution
{
public:
ListNode* removeNthFromEnd(ListNode* head, int n)
{
ListNode** t1 = &head, *t2 = head;
for(int i = 1; i < n; ++i)
{
t2 = t2->next;
}
while(t2->next != NULL)
{
t1 = &((*t1)->next);
t2 = t2->next;
}
*t1 = (*t1)->next;
return head;
}
};

这份代码和我所实现的区别在于使用了二级指针,开始觉得很莫名其妙,后来看了Linus:利用二级指针删除单向链表的解释后,感觉这种做法挺巧妙的。

在第一种方法中(不使用二级指针),我所做的操作是将entry->next赋值给prev->next,然而当entry为链表头的时候就没有prev->next可以赋值了。但是实际上,任何一个链表都会有一个和prev->next同等地位的指针,那就是head指针,对于链表头元素,执行的操作可以是将head->next赋值给head。此时第二种方法(使用二级指针)的好处是它不关心entry->next究竟赋值给了谁,只关心赋值的地址,因此也免了判断entry是不是head的步骤了。

(微软笔试崩了……郁闷)

LeetCode题解:(19) 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

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

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

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

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

  5. 【一天一道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 ...

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

  7. leetcode个人题解——#19 Remove Nth Node From End of List

    思路:设置两个指针,其中第二个指针比第一个延迟n个元素,这样,当第二个指针遍历到指针尾部时,对第一个指针进行删除操作. 当然,这题要注意一些边界值,比如输入[1,2] n=2时如果按照思路走会指向未分 ...

  8. [Leetcode][Python]19: Remove Nth Node From End of List

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 38: Count and Sayhttps://oj.leetcode.co ...

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

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

  10. 【LeetCode】19. Remove Nth Node From End of List

    题目: 思路:如果链表为空或者n小于1,直接返回即可,否则,让链表从头走到尾,每移动一步,让n减1. 1.链表1->2->3,n=4,不存在倒数第四个节点,返回整个链表 扫过的节点依次:1 ...

随机推荐

  1. 1-[并发编程]-操作系统OS

    1.为什么要有操作系统 现代的计算机系统主要是由一个或者多个处理器,主存,硬盘,键盘,鼠标,显示器,打印机,网络接口及其他输入输出设备组成. 一般而言,现代计算机系统是一个复杂的系统. 其一:如果每位 ...

  2. 04 - django的路由层 1

    1.路由控制简单配置 from django.conf.urls import url from . import views urlpatterns = [ url(r'^articles/2003 ...

  3. Gitlab+Jenkins学习之路(二)之gitlab部署

    1.安装依赖及gitlab [root@linux-node1 ~]# yum install -y curl policycoreutils openssh-server openssh-clien ...

  4. Oracle 表备份还原

    方法1: create table mdmuser20120801 as select * from mdmuser   方法2: create table mdmuser20120801 as se ...

  5. [SDOI2018]战略游戏 圆方树,树链剖分

    [SDOI2018]战略游戏 这题是道路相遇(题解)的升级版,询问的两个点变成了\(S\)个点. LG传送门 还是先建出圆方树,考虑对于询问的\(S\)个点,答案就是圆方树上能包含这些点的最小连通块中 ...

  6. jsp编译原理

    jsp运行时都要先转换成servlet,使用tomcat时会在tomcat安装目录下的work生成一系列的运行的项目文件夹,文件下面含有.java文件和编译后的.class文件.jsp最终转化为ser ...

  7. 曾经的华为C面试题,一点就通

     学习编程可以锻炼你的思维,帮助你更好地思考,创建一种我认为在各领域都非常有用的思维方式.   比尔盖茨      曾经的华为C面试题,一点就通 [问题区] 有两个变量x和y, x=10; y = 2 ...

  8. python函数式编程,性能,测试,编码规范

    这篇文章主要是对我收集的一些文章的摘要.因为已经有很多比我有才华的人写出了大量关于如何成为优秀Python程序员的好文章. 我的总结主要集中在四个基本题目上:函数式编程,性能,测试,编码规范.如果一个 ...

  9. python的字符串格式化

    1.python到底有那几种字符串格式化模块? python有3种格式化字符串的方法: 传统的%字符串格式符 str.format函数 字符串模版template 新的python 3.6+还提供了新 ...

  10. GitHub笔记(三)——分支管理和多人协作

    三.分支管理 0 语句: 查看分支:git branch 创建分支:git branch <name> 切换分支:git checkout <name> 创建+切换分支:git ...