# 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
"""
dumpy_head = ListNode(next = head)
slow, fast = dumpy_head, dumpy_head
for _ in range(n+1):
fast = fast.next
while(fast != None):
fast = fast.next
slow = slow.next
slow.next = slow.next.next
return dumpy_head.next
19. 删除链表的倒数第 N 个结点
最新推荐文章于 2024-11-08 21:29:49 发布
本文介绍了如何使用Python实现从单链表中移除指定位置的节点,通过定义ListNode类和Solution类,展示了removeNthFromEnd方法的具体步骤。重点在于理解双指针技巧在解决此类问题中的应用。
摘要由CSDN通过智能技术生成