LeetCode 19. Remove Nth Node From End of List 解答

视频讲解

代码

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
        

        # create dummy head
        dummy_head = ListNode(0)
        dummy_head.next = head
        
        # create two pointers left and right
        
        left = dummy_head
        right = dummy_head
        for _ in range(n):
            right = right.next
        
        # shift left and right
        while right.next:
            left = left.next
            right = right.next
        
        # delete prev.next
        prev = left
        succ = left.next.next
        prev.next = succ
        
        # return
        return dummy_head.next

PPT讲解

本题涉及的知识点:

我的 Leetcode 讲解频道

代码链接

面试高频考点Youtube链接

面试低频考点Youtube链接

站内搜索 | Search


    视频 | Video Links

    Table of Contents