[leetcode019] Remove Nth Node From End of List 解题报告

[leetcode019] 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 1->2->3->5.

Note:

Given n will always be valid.

题目大意是删除单链表倒数第N个节点,并假设N是合法的。

双指针是单链表问题常用的方法。如果要删除第N个节点,可以让一个指针先向前走N步,然后另一个指针从头开始,直到第一个指针走到最后一个节点。则另一个指针所在位置的下一个位置即为需要删除的节点。这个题目比较简单,但是需要注意待删除节点如果是第一个节点这种情况。

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

class Solution:
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        fast = slow = head
        # fast指针向前走N步
        for _ in range(n):
            fast = fast.next
        # 如果fast指针为空,说明待删除节点是第一个节点
        if fast is None:
            return head.next

        # 两个指针同时开始走
        while fast.next is not  None:
            fast = fast.next
            slow = slow.next
        # 删除下一个节点
        slow.next = slow.next.next
        return head
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值