Leetcode 分享筆記

19. Remove Nth Node From End of List
等級: Medium
原始題目連結請點此
leetcode
# 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
        """
        runner = head
        for i in range(n):
            runner = runner.next
        dummy = walker = ListNode(next=head)
        
        while runner:
            walker = walker.next
            runner = runner.next
        walker.next = walker.next.next
        return dummy.next