Leetcode 中等:19. 删除链表的倒数第N个节点

题目:删除链表的倒数第N个节点

  • 题号:19
  • 难度:中等
  • https://leetcode-cn.com/problems/remove-nth-node-from-end-of-list/

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

示例1

输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]

示例2

输入:head = [1], n = 1
输出:[]

示例3

输入:head = [1,2], n = 1
输出:[1]

提示

  • 链表中结点的数目为 sz
  • 1 <= sz <= 30
  • 0 <= Node.val <= 100
  • 1 <= n <= sz

进阶

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


实现

第一种:先求链表长度的方法

思路:先求出链表的长度len,再求出要删除结点的位置index = len - n。这样就可以从头结点开始遍历到该位置,删除该结点即可。

C# 语言

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */

public class Solution
{
    public ListNode RemoveNthFromEnd(ListNode head, int n)
    {
        int len = GetLength(head);
        int index = len - n;

        if (index == 0)
        {
            head = head.next;
            return head;
        }
        ListNode temp = head;
        for (int i = 0; i < index - 1; i++)
        {
            temp = temp.next;
        }
        temp.next = temp.next.next;
        return head;
    }

    public int GetLength(ListNode head)
    {
        ListNode temp = head;
        int i = 0;
        while (temp != null)
        {
            i++;
            temp = temp.next;
        }
        return i;
    }
}

Python 语言

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        index = self.GetLength(head) - n
        if index == 0:
            head = head.next
            return head
        temp = head
        for i in range(index - 1):
            temp = temp.next
        temp.next = temp.next.next
        return head

    def GetLength(self, head):
        temp = head
        i = 0
        while temp != None:
            i += 1
            temp = temp.next
        return i

第二种:利用双指针的方式

思路: 使用两个指针,前面的指针p1先走n步,接着让后面的指针p2p1同步走,p1走到终点,p2即走到要移除的结点位置。

C# 语言

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
public class Solution 
{
    public ListNode RemoveNthFromEnd(ListNode head, int n) 
    {
        ListNode p1 = head;
        ListNode p2 = head;
        
        while (n > 0)
        {
            p1 = p1.next;
            n--;
        }

        if (p1 == null) //移除头结点
        {
            return head.next;
        }
            
        while (p1.next != null)
        {
            p1 = p1.next;
            p2 = p2.next;
        }
            
        p2.next = p2.next.next;
        return head;
    }
}

Python 语言

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        p2 = head
        p1 = head
        while (n > 0):
            p1 = p1.next
            n -= 1

        if (p1 is None):  # 移除头结点
            return head.next

        while (p1.next):
            p2 = p2.next
            p1 = p1.next

        p2.next = p2.next.next
        return head
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青少年编程小助手_Python

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值