zoukankan      html  css  js  c++  java
  • Remove Nth Node From End of List

     1 # Definition for singly-linked list.
     2 # class ListNode(object):
     3 #     def __init__(self, x):
     4 #         self.val = x
     5 #         self.next = None
     6 
     7 class Solution(object):
     8     def removeNthFromEnd(self, head, n):
     9         """
    10         :type head: ListNode
    11         :type n: int
    12         :rtype: ListNode
    13         """
    14         if not head:
    15             return None
    16         dummy=ListNode(-1)
    17         dummy.next=head
    18 
    19         for i in range(n):
    20             if(head):
    21                 head=head.next
    22             else:
    23                 return None
    24         
    25         if(head==None):
    26             return dummy.next.next
    27         
    28         slow=dummy.next
    29         while(head.next):
    30             head=head.next
    31             slow=slow.next
    32         
    33         slow.next=slow.next.next
    34 
    35         return dummy.next
  • 相关阅读:
    重排列
    最多分成多少块
    后面第一个大于
    走格子
    硬币游戏
    还是01串
    戴德兰
    个人所得税
    最长高地
    执行Commit时Oracle做哪些工作
  • 原文地址:https://www.cnblogs.com/zijidan/p/12535439.html
Copyright © 2011-2022 走看看