题目

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.

代码:9ms过集合

/**
* 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(-);
dummy.next = head;
ListNode *p1 = &dummy, *p2 = &dummy;
for (size_t i = ; i < n; ++i, p2=p2->next);
for (; p2->next; p1=p1->next, p2=p2->next);
ListNode *tmp = p1->next;
p1->next = p1->next==NULL ? NULL : p1->next->next;
delete tmp;
return dummy.next;
}
};

Tips:

双指针技巧:

  a. p2先走n步

  b. p1和p2一起走,直到p2走到最后一个元素

  c. 删除元素

注意再删除元素的时候,保护一下p1->next指针不为NULL。

==========================================

第二次过这道题,思路比较清晰。由于受到Rotate List这道题的影响,第一次把p1 = head 和 p2 = head了;之后改成了p1 = &dummpy和p2 = &dummpy就AC了。

/**
* 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 dummpy(-);
dummpy.next = head;
ListNode* p1 = &dummpy;
ListNode* p2 = &dummpy;
for ( int i=; i<n; ++i ) p2 = p2->next;
while ( p2 && p2->next)
{
p1 = p1->next;
p2 = p2->next;
}
p1->next = p1->next ? p1->next->next : NULL;
return dummpy.next;
}
};

【Remove Nth Node From End of List】cpp的更多相关文章

  1. leetcode 【 Remove Nth Node From End of List 】 python 实现

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

  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. 63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List

    Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For exam ...

  4. LeetCode: Remove Nth Node From End of List 解题报告

    Remove Nth Node From End of List Total Accepted: 46720 Total Submissions: 168596My Submissions Quest ...

  5. Merge Two Sorted Lists & Remove Nth Node From End of List

    1.合并两个排好序的list Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The ...

  6. leetcode-algorithms-19 Remove Nth Node From End of List

    leetcode-algorithms-19 Remove Nth Node From End of List Given a linked list, remove the n-th node fr ...

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

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

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

  9. LeetCode解题报告—— 4Sum & Remove Nth Node From End of List & Generate Parentheses

    1. 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + ...

随机推荐

  1. python3基础07(进程操作及执行系统级命令等)

    #subprocess 创建子进程 连接输入 输出 管道错误,及获取他们的状态,可执行操作系统级的命令# subprocess.run(args, *, stdin=None, input=None, ...

  2. linux 命令——38 cal (转)

    cal命令可以用来显示公历(阳历)日历.公历是现在国际通用的历法,又称格列历,通称阳历.“阳历”又名“太阳历”,系以地球绕行太阳一周为一年,为西方各国所通用,故又名“西历”. 1.命令格式: cal  ...

  3. 【BZOJ4810】[YNOI2017] 由乃的玉米田(莫队+bitset)

    点此看题面 大致题意: 给你一段序列,每次询问一段区间内是否存在两个数的差或和或积为\(x\). 莫队算法 看到区间询问+可以离线,首先想到了莫队啊. 但是,在较短的时间内更新信息依然比较难以实现. ...

  4. [论文理解]MetaAnchor: Learning to Detect Objects with Customized Anchors

    MetaAnchor: Learning to Detect Objects with Customized Anchors Intro 本文我其实看了几遍也没看懂,看了meta以为是一个很高大上的东 ...

  5. python_14_sys_mod

    import sys #1 print(sys.path)#打印环境变量 #2 print(sys.argv)#打印相对路径 print(sys.argv[2])#在cmd命令窗口运行本文件

  6. 【luogu P5022 旅行】 题解

    题目连接:https://www.luogu.org/problemnew/show/P5022 \(NOIP2018 DAY2T1\) 考场上只写了60分,很容易想到当 m = n - 1 时的树的 ...

  7. bat 服务启动脚本

    当电脑上有多个数据库(特别是Oracle,占用内存大,所以我都是设置为手动启动的,或者想在电脑上运行一下其他UI类软件或玩些游戏的时候也需要暂时关掉,奈何我这渣机(V_V))需要启动或停止的时候,就用 ...

  8. Spring Cloud学习介绍

    最近在学spring cloud, 整理了下 简单知识要求: 1.要了解springboot 2.了解分布式架构 3.了解微服务 4.了解springcloud是做什么的 带着这些,初学者 就至少有个 ...

  9. mysql 查询出 n小时 以前的数据

    select * FROM biaoming WHERE TIMESTAMPDIFF(SECOND ,CREATE_TIME,now() ) > 3*60*60

  10. Linux dmidecode命令

    1.linux系统自带的dmidecode工具查询服务器硬件信息 dmidecode 用于获取服务器的硬件信息,通常是在不打开计算机机箱的情况下使用该命令来查找硬件详细信息 这个命令可以查看内存的几乎 ...