zoukankan      html  css  js  c++  java
  • 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,

     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.

    算法

    代码一

     1 public ListNode removeNthFromEnd(ListNode head, int n) {
     2         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
     3         ListNode p = head, q;
     4         int index = 1;
     5         while (p.next != null) {
     6             map.put(index, p.val);
     7             index++;
     8             p = p.next;
     9         }
    10         map.put(index, p.val);
    11         int key = index + 1 - n;
    12         int val = map.get(index + 1 - n);
    13         p = q = head;
    14         for (int i = 1; i < key; i++) {
    15             q = p;
    16             p = p.next;
    17         }
    18         if (p == q)
    19             head = p.next;
    20         else {
    21             q.next = p.next;
    22         }
    23         return head;
    24     }

    代码二

     1 /**
     2  * 
     3  * @author audr00
     4  * A one pass solution can be done using pointers. Move one pointer fast --> n+1 places forward, 
     5  * to maintain a gap of n between the two pointers and then move both at the same speed. Finally, 
     6  * when the fast pointer reaches the end, the slow pointer will be n+1 places behind - just the right
     7  * spot for it to be able to skip the next node.
     8  * Since the question gives that n is valid, not too many checks have to be put in place. Otherwise, 
     9  * this would be necessary.
    10  *
    11  */
    12     public ListNode removeNthFromEnd(ListNode head, int n) {
    13 
    14         ListNode start = new ListNode(0);
    15         ListNode slow = start, fast = start;
    16         slow.next = head;
    17 
    18         //Move fast in front so that the gap between slow and fast becomes n
    19         for(int i=1; i<=n+1; i++)   {
    20             fast = fast.next;
    21         }
    22         //Move fast to the end, maintaining the gap
    23         while(fast != null) {
    24             slow = slow.next;
    25             fast = fast.next;
    26         }
    27         //Skip the desired node
    28         slow.next = slow.next.next;
    29         return start.next;
    30     }
    31     public static void main(String[]args){
    32         //new RemoveNthFromEnd1().removeNthFromEnd(head, n)
    33     }
  • 相关阅读:
    Gmail、还有人需要吗?
    Google 打不开
    不能忽视的Doctype
    ASP.NET2.0中用ICallbackEventHandler实现客户端与服务器端异步交互
    DataGrid常用小技巧
    ASP.NET程序安全性(三) 表单提交、过滤用户输入
    Objection!!!
    编写3dmax插件需要注意的几个问题
    又一个IGame的bug
    VC2010中的C++0x特性 Part 1:Lambdas,auto, static_assert
  • 原文地址:https://www.cnblogs.com/qiaoshanzi/p/4955904.html
Copyright © 2011-2022 走看看