美文网首页
2020-03-03 Day10 Leetcode: 19. R

2020-03-03 Day10 Leetcode: 19. R

作者: YueTan | 来源:发表于2020-03-03 08:44 被阅读0次
# 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
            

相关文章

网友评论

      本文标题:2020-03-03 Day10 Leetcode: 19. R

      本文链接:https://www.haomeiwen.com/subject/ubmflhtx.html