首页 > 其他 > 详细

Remove Nth Node From End of List

时间:2020-03-21 02:35:12      阅读:40      评论:0      收藏:0      [点我收藏+]

 

 

 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

 

Remove Nth Node From End of List

原文:https://www.cnblogs.com/zijidan/p/12535439.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!