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

    链接:  http://leetcode.com/problems/remove-nth-node-from-end-of-list/

    题解:

    one pass remove Nth node from the end。要建立一个dummy node,用来记录所要移除节点之前的节点,再利用先后指针就可以找到这个节点了。

    Time Complexity - O(n), Space Complexity - O(1)。

    public class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            if(head == null)
                return head;
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode first = dummy;      //because n is always valid, so we don't need to get the length of the linked list and do mod to n
            ListNode second = dummy;
            
            while(first.next != null){
                first = first.next;
                n--;
                if(n < 0)
                    second = second.next;
            }
            
            second.next = second.next.next;
            return dummy.next;
        }
    }

     

    二刷:

    要计算好是哪一个节点。

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode fast = dummy, slow = dummy;
            while (fast.next != null) {
                fast = fast.next;
                if (n <= 0) {
                    slow = slow.next;    
                }
                n--;
            }
            slow.next = slow.next.next;
            return dummy.next;
        }
    }

    Python:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def removeNthFromEnd(self, head, n):
            """
            :type head: ListNode
            :type n: int
            :rtype: ListNode
            """
            dummy = ListNode(-1)
            dummy.next = head
            slow = dummy
            fast = dummy
            while fast.next != None:
                fast = fast.next
                if n <= 0:
                    slow = slow.next
                n -= 1
            slow.next = slow.next.next
            return dummy.next

    三刷: 

    用个例子就很容易能算出来是哪个节点了。  List = {1, 2, 3}, n = 2

    Java:

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode removeNthFromEnd(ListNode head, int n) {
            ListNode dummy = new ListNode(-1);
            dummy.next = head;
            ListNode slow = dummy, fast = dummy;
            while (fast.next != null) {
                fast = fast.next;
                if (n <= 0) {
                    slow = slow.next;
                }
                n--;
            }
            slow.next = slow.next.next;
            return dummy.next;
        }
    }

    Reference:

    https://leetcode.com/discuss/37149/3-short-python-solutions

  • 相关阅读:
    深度学习(机器学习)tensorflow环境配置及安装
    深度学习(机器学习)tensorflow学习第一课——tensor数据载体中的基本数据类型
    学生选课小项目——麻雀虽小,五脏俱全(Templates)
    素数定理简史
    关于全栈岗位及其成员轮岗问题的思考
    Spring WebFlux 简单业务代码及其Swagger文档
    Spring 5 中函数式webmvc开发中的swagger文档
    创建基于kotlin开发环境的spring项目入门
    折射定律的来历简介
    朴素贝叶斯方法入门
  • 原文地址:https://www.cnblogs.com/yrbbest/p/4434491.html
Copyright © 2011-2022 走看看