zoukankan      html  css  js  c++  java
  • leetcode19:删除链表的倒数第N个节点

    给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。

    示例:

    给定一个链表: 1->2->3->4->5, 和 n = 2.

    当删除了倒数第二个节点后,链表变为 1->2->3->5.
    说明:

    给定的 n 保证是有效的。

    进阶:

    你能尝试使用一趟扫描实现吗?

    =====================================Python=================================

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
            dummy = ListNode(0)
            dummy.next = head
            if head is None:
                return None
    
            if head.next is None:
                if n == 0:
                    return head
                return None 
    
            slow = dummy
            fast = dummy
    
            for i in range(n):
                fast = fast.next
    
            while fast.next:
                fast = fast.next
                slow = slow.next
            
            slow.next = slow.next.next
    
            return dummy.next
    
    ------------------------------------------------
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
            dummyNode = ListNode(None)
            dummyNode.next = head
            cur = dummyNode
            pre = None
            fast = dummyNode
            for _ in range(n):
                fast = fast.next
            while fast:
                fast = fast.next
                pre = cur
                cur = cur.next
            pre.next = cur.next
            return dummyNode.next

    ==============================================Java======================================================

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

    ================================================Go=============================================

    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func removeNthFromEnd(head *ListNode, n int) *ListNode {
        result := &ListNode{}
        result.Next = head
        var pre *ListNode
        cur := result
        i := 1
        for head != nil {
            if i >= n {
                pre = cur
                cur = cur.Next
            }
            head = head.Next
            i++
        }
        pre.Next = pre.Next.Next
        return result.Next
    }
  • 相关阅读:
    CNN网络架构演进:从LeNet到DenseNet
    【OCR技术系列之四】基于深度学习的文字识别(3755个汉字)
    【OCR技术系列之三】大批量生成文字训练集
    【Keras】基于SegNet和U-Net的遥感图像语义分割
    Python各类图像库的图片读写方式总结
    我的2017:从工作再到学生
    VS2017 WinFrom打包设置与教程
    采用Opserver来监控你的ASP.NET项目系列(三、监控你的服务器状态)
    采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)
    采用Opserver来监控你的ASP.NET项目系列(一、Opserver监控的简介与平台搭建)
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13495165.html
Copyright © 2011-2022 走看看