题目

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. zip man man.config

    zip man man.config zip -r zip1 man.config man.zip gzip a tar -cvf test.tar /home/* tar -tf test.tar ...

  2. 线程池模块thernd

    from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor import time def task(i): print ...

  3. k8s之configmap配置中心

    记录在石墨笔记中,懒得再粘贴了,大家直接移步下面地址 https://shimo.im/docs/ktNM72QPweEEkcWg/

  4. cms系统-帖子页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. LeetCode Unique Binary Search Trees (DP)

    题意: 一棵BST有n个节点,每个节点的key刚好为1-n.问此树有多少种不同形态? 思路: 提示是动态规划. 考虑一颗有n个节点的BST和有n-1个节点的BST.从n-1到n只是增加了一个点n,那么 ...

  6. IOS 自定义Layer(图层)

    方式1: @interface NJViewController () @end @implementation NJViewController - (void)viewDidLoad { [sup ...

  7. 【BZOJ3930】[CQOI2015] 选数(容斥)

    点此看题面 大致题意: 让你求出在区间\([L,H]\)间选择\(n\)个数时,有多少种方案使其\(gcd\)为\(K\). 容斥 原以为是一道可怕的莫比乌斯反演题. 但是,数据范围中有这样一句话:\ ...

  8. 【BZOJ1076】[SCOI2008] 奖励关(状压DP)

    点此看题面 大致题意:总共有\(n\)个宝物和\(k\)个回合,每个回合系统将随机抛出一个宝物(抛出每个宝物的概率皆为\(1/n\)),吃掉一个宝物可以获得一定的积分(积分可能为负),而吃掉某个宝物有 ...

  9. Java代码工具箱之链接Oracle

    1. 需要oracle的 odbc  jar包 2. 代码 3. 注意:ps对象和statement对象最好用完立即释放,尤其是读写数据库代码出现在 for 循环语句中时. 否则会出现游标不够的情况, ...

  10. windows下安装win7虚拟机并安装oracle

    一.win7虚拟机 与安装linux虚拟机没有什么不同,不同的是选择客户机操作系统.内存.磁盘容量,以及映像文件. 创建win7虚拟机步骤简化: 新建虚拟机-->>自定义-->> ...