2020-03-03 Day10 Leetcode: 19. R

2020-03-03  本文已影响0人  YueTan
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:        
        fast=head
        i=0
        while i<n:
            fast=fast.next
            i+=1
        
        if fast is None:
            return  head.next            
            
        slow=head
        while fast.next!=None:
            slow=slow.next
            fast=fast.next
            
        slow.next=slow.next.next            
        return head
            
上一篇 下一篇

猜你喜欢

热点阅读