19. Remove Nth Node From End of List 倒数删除链表元素

思路:第一时间想到的肯定是通过添加计数器解决这个问题,但是问题产生了,链表具有单向性,统计总数,还是计算当前数目都在效率上面很吃亏。

借用快慢指针的思想。可以安排两个同速指针,但是他们存在先后顺序,就可以解决这个问题。

public class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        if(head == null || n <= 0) return null;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode pre = dummy;
        ListNode index = dummy.next;
        int count = n;
        while(count > 0)
        {
            index = index.next;
            count--;
        }
        while(index != null)
        {
            index = index.next;
            pre = pre.next;
        }
        pre.next =  pre.next.next;
        return dummy.next;
    }
}
时间: 2024-12-11 15:43:05

19. Remove Nth Node From End of List 倒数删除链表元素的相关文章

LeetCode 19. Remove Nth Node From End of List(删除链表中倒数第N个节点)

题意:删除链表中倒数第N个节点. 法一:递归.每次统计当前链表长度,如果等于N,则return head -> next,即删除倒数第N个节点:否则的话,问题转化为子问题“对head->next这个链表删除倒数第N个节点”,将head的next指针指向该子问题的结果,返回head即可.这个方法时间复杂度高,不推荐. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

25.Remove Nth Node From End of List(删除链表的倒数第n个节点)

Level: ??Medium 题目描述: Given a linked list, remove the n-th node from the end of list and return its head. 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-

[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, 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:  Give

[Leetcode][Python]19: Remove Nth Node From End of List

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 19: Remove Nth Node From End of Listhttps://oj.leetcode.com/problems/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 ex

61. Rotate List(M);19. Remove Nth Node From End of List(M)

61. Rotate List(M) Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. Total Accepted: 102574 Total Submissions: 42333

leetCode 19. Remove Nth Node From End of List 链表

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 lis

19. Remove Nth Node From End of List(js)

19. Remove Nth Node From End of List Given a linked list, remove the n-th node from the end of list and return its head. Example: Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes

leetCode 19.Remove Nth Node From End of List(删除倒数第n个节点) 解题思路和方法

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

19. Remove Nth Node From End of List(C++)

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. Solution : 1