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,

  1. Given linked list: 1->2->3->4->5, and n = 2.
  2.  
  3. 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啦!

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

  1. /**
  2. * Definition for singly-linked list.
  3. * public class ListNode {
  4. * int val;
  5. * ListNode next;
  6. * ListNode(int x) {
  7. * val = x;
  8. * next = null;
  9. * }
  10. * }
  11. */
  12. public class Solution {
  13. public ListNode removeNthFromEnd(ListNode head, int n) {
  14. //
  15. ListNode dummy = new ListNode();
  16. dummy.next = head;
  17.  
  18. ListNode slow = dummy;
  19. ListNode fast = dummy;
  20.  
  21. // move fast N more than slow.
  22. while (n > ) {
  23. fast = fast.next;
  24. // Bug 1: FORGET THE N--;
  25. n--;
  26. }
  27.  
  28. while (fast.next != null) {
  29. fast = fast.next;
  30. slow = slow.next;
  31. }
  32.  
  33. // Slow is the pre node of the node which we want to delete.
  34. slow.next = slow.next.next;
  35.  
  36. return dummy.next;
  37. }
  38. }

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. 6.4 APK包限制

    Google 2015年 9月28日公告:为了Android开发商可以制作出更加复杂的app和游戏,Google Play游戏包体(APK)大小由原来的50MB提高到100MB.但是针对Android ...

  2. Xshell_Using X11 forwarding

    FROM:http://www.netsarang.com/tutorial/xshell/1018/Using_X11_forwarding The X11 forwarding feature i ...

  3. Rsync 3.1.0 发布,文件同步工具

    文件同步工具Rsync 3.1.0发布.2013-09-29 上一个版本还是2011-09-23的3.0.9 过了2年多.Rsync基本是Linux上文件同步的标准了,也可以和inotify配合做实时 ...

  4. 解决linux下oracle-11g打不开dbca问题

    linux下oracle安装完毕后,出现建立数据库命令dbca无法使用问题,如图: 解决办法: 在32位的linux环境下,安装32位oracle11g会有这个bug,主要装个补丁(patch nam ...

  5. Mac OS X 安装ruby环境

    1.查看版本 $ ruby -v ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14] 2.查看源 $ gem ...

  6. [51单片机] EEPROM 24c02 + 数码管 + 中断 [统计开机次数]

    >_<:24c02的SCL连P2.0;SDA连P2.1;WP接GND;P0接8位数码管的8针;P2.2连段码;P2.3连位码; >_<:delay.c #include &qu ...

  7. 虚拟机VirtualBox中centos6.5网络设置

    一.虚拟机网络配置 默认只是设置了网卡1:方式NAT(对应ifcfg-eth0) 我们还可以设置网卡2,网卡3.可以在系统安装完成后设置. 网卡2设置回环网卡,实现虚拟机与宿主机组成局域网(对应ifc ...

  8. [数据库操作]Java中的JDBC的使用方法.

    前言:想必大家在实际编码中都遇到过JDBC的操作, 这里仅做自己的一个总结, 有错误和不完整之处还请大家提出来. 1,JDBC其实一套规范(接口)数据库厂商需要实现此接口(实现类)--数据库驱动 2, ...

  9. [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler

    [原创]推荐一款强大的.NET程序内存分析工具.NET Memory Profiler 1 官方网站:http://memprofiler.com/2 下载地址:http://memprofiler. ...

  10. js 生成笛卡尔积

    其实生成 笛卡尔积的方法原本很简单,for循环就可以了, function discarts() { //笛卡尔积 var twodDscartes = function (a, b) { var r ...