题目

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. git push 使用教程

    git push命令用于将本地分支的更新,推送到远程主机.它的格式与git pull命令相仿. $ git push <远程主机名> <本地分支名>:<远程分支名> ...

  2. VM安装centOS6.9

    1.首先要下载一个centos的iso镜像,用VMware创建一个空白硬盘. 2.创建完毕再设置里面挂载iso的centos系统文件. 3.进入到这个页面: 说明: ①install or upgra ...

  3. 2018.1.30 PHP编程之验证码

    PHP编程之验证码 1.创建验证码函数 验证码函数输入通用函数,将函数放入global.func.php里. //创建一个随机码 for($ i=0;$i<4;$i++){ $_nmsg. = ...

  4. os.walk 模块

    os.walk()可以得到一个三元tupple(dirpath, dirnames, filenames),其中第一个为起始路径,第二个为起始路径下的文件夹,第三个是起始路径下的文件. 其中dirpa ...

  5. MicroService 微服务提供者搭建

    本机IP为  192.168.1.102 1.  新建Maven项目   microservice 2.   pom.xml <project xmlns="http://maven. ...

  6. Debug与Release版本的区别详解

    原文链接 Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动.如果我们愿意,我们完全可以把Debug和Release的行为完全颠 ...

  7. CUDA && GPU中dim3介绍

  8. 操作文件 -------JavaScrip

    本文摘要:http://www.liaoxuefeng.com/ 在HTML表单中,可以上传文件的唯一控件就是<input type="file">. 注意:当一个表单 ...

  9. 图片url转base64

    var xhr = new XMLHttpRequest() // 配置的代理,解决跨域问题 xhr.open('GET', url.replace('http://xxx.com', '/img') ...

  10. MySQL详细安装过程

    目录 一.概述 二.MySQL安装 三.安装成功验证 四.NavicatforMySQL下载及使用 一.概述 MySQL版本:5.7.17 下载地址:http://rj.baidu.com/soft/ ...