首页 > 其他 > 详细

leetcode——19. 删除链表的倒数第N个节点

时间:2019-10-24 01:37:51      阅读:70      评论:0      收藏: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:
        if head.next==None and n==1:
            return 
        a=[]
        while head:
            a.append(head.val)
            head=head.next
        #print(a)
        #print(len(a))
        a.pop(len(a)-n)
        head=ListNode(a[0])
        p=head
        for i in range(1,len(a)):
            p.next=ListNode(a[i])
            p=p.next
        return head
执行用时 :44 ms, 在所有 python3 提交中击败了86.21%的用户
内存消耗 :13.8 MB, 在所有 python3 提交中击败了5.53%的用户
 
                                                                              ——2019.10.23

leetcode——19. 删除链表的倒数第N个节点

原文:https://www.cnblogs.com/taoyuxin/p/11729556.html

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