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
    }
  • 相关阅读:
    包和常用内置模块(二)
    常用内置模块(一)
    正则表达式和re模块
    迭代器和生成器
    函数(四)
    函数(三)闭包函数与装饰器
    Codeforces Round #539 (Div. 2) D 思维
    Codeforces Round #539 (Div. 2) 异或 + dp
    Codeforces Round #546 (Div. 2) E 推公式 + 线段树
    牛客练习赛42 C 反着计算贡献
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13495165.html
Copyright © 2011-2022 走看看