Remove Nth Node From End of List

Total Accepted: 46720 Total Submissions: 168596My Submissions

Question Solution 

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.

Show Tags
 

SOLUTION 1:

1.使用快慢指针,快指针先行移动N步。用慢指针指向要移除的Node的前一个Node.

2. 使用dummy node作为head的前缀节点,这样就算是删除head也能轻松handle啦!

主页君是不是很聪明呀? :)

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
//
ListNode dummy = new ListNode();
dummy.next = head; ListNode slow = dummy;
ListNode fast = dummy; // move fast N more than slow.
while (n > ) {
fast = fast.next;
// Bug 1: FORGET THE N--;
n--;
} while (fast.next != null) {
fast = fast.next;
slow = slow.next;
} // Slow is the pre node of the node which we want to delete.
slow.next = slow.next.next; return dummy.next;
}
}

GITHUB (国内用户可能无法连接):

https://github.com/yuzhangcmu/LeetCode/blob/251766ffb832f2278f43a05e194ca76584bf14ea/list/RemoveNthFromEnd.java

LeetCode: Remove Nth Node From End of List 解题报告的更多相关文章

  1. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

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

  2. [leetcode]Remove Nth Node From End of List @ Python

    原题地址:http://oj.leetcode.com/problems/remove-nth-node-from-end-of-list/ 题意: Given a linked list, remo ...

  3. LeetCode——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 ...

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

  5. [LeetCode] 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] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  7. leetcode remove Nth Node from End python

    # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = ...

  8. LeetCode Remove Nth Node From End of List 删除链表的倒数第n个结点

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  9. LeetCode 237 Delete Node in a Linked List 解题报告

    题目要求 Write a function to delete a node (except the tail) in a singly linked list, given only access ...

随机推荐

  1. my sql

    如果改了上面的4个配置文件,要让其立即生效,可以使用如下方法 source .bash_profile . .bash_profile 基于Apache+php+mysql的许愿墙网站的搭建 方案一: ...

  2. 【12_206】Reverse Linked List

    本来没想出来,刚才突然想到,可以用“头插法”来反转 Reverse Linked List My Submissions Question Total Accepted: 66556 Total Su ...

  3. mongoDB研究笔记:复制集数据同步机制

    http://www.cnblogs.com/guoyuanwei/p/3279572.html  概述了复制集,整体上对复制集有了个概念,但是复制集最重要的功能之一数据同步是如何实现的?带着这个问题 ...

  4. Beetl 1.25 发布,java模板引擎

    Beetl 1.25 发布改进记录包括: Beetl支持同JS一样三元逻辑表达式,如:    var c = a>1?’li’;’miao’;    var d = b>1?:’ok’; ...

  5. centos7 安装 notejs

    1.安装集成工具 yum -y install gcc make gcc-c++ 2.安装notejs 自行选择版本:https://nodejs.org/dist/ wget https://nod ...

  6. 除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法启动网站。目前,这两项服务均处于停止状态。

    win7 IIS 所有网站都停止了,启动提示: 除非 Windows Activation Service (WAS)和万维网发布服务(W3SVC)均处于运行状态,否则无法启动网站.目前,这两项服务均 ...

  7. eclipse导入Android项目后,项目的名称变为了主Activity的名称

    不要使用File -> Import -> Android -> Existing Android Code into Workspace,而是用 File -> Import ...

  8. Nginx运行Mono Web (ASP.NET)

    Mono Web除了可以使用Apache/mod_mono方式承载运行外,还可以使用Nginx/FastCGI方式运行. Nginx配置asp.net更简单方便,用处也多,可以通过FastCGI运行a ...

  9. Scala 中 构造函数,重载函数的执行顺序

    在调试scala在线开发教程(http://www.imobilebbs.com/wordpress/archives/4911)的过程中看到了以下代码,但是这段代码无论怎么调试都无法成功. abst ...

  10. EXCEL VBA入门篇之代码应用基础