LeetCode 19. [Python] Remove Nth Node From End of List
原题链接
中等
作者:
徐辰潇
,
2021-02-20 10:05:48
,
所有人可见
,
阅读 284
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
#Use slow and fast pointer
dummy = ListNode(-1)
dummy.next = head
fast = head
for i in range(n):
fast = fast.next
slow = head
slow_prev = dummy
while fast:
slow_prev = slow
slow = slow.next
fast = fast.next
slow_prev.next = slow.next
return dummy.next