Remove Nth Node From End of List - LeetCode

来源:互联网 发布:好看的喜剧电影知乎 编辑:程序博客网 时间:2024/06/05 08:31

Remove Nth Node From End of List - LeetCode

题目:

Given a linked list, remove the nth node from the end of list and return its head.

For example,

   Given linked list: 1->2->3->4->5, and n = 2.   After removing the second node from the end, the linked list becomes 1->2->3->5.

分析:

纯考验链表的题目。感觉一句话占用分析好坑啊。。

代码:

class Solution:    # @return a ListNode    def removeNthFromEnd(self, head, n):        l,l1,prev = head,head,head        count,i = 0,0        while head:            count += 1            head = head.next        if count-n == 0:            l1 = l.next            return l1        while l:            if i == count - n:                prev.next = l.next                return l1            i+=1            prev,l = l,l.next



0 0
原创粉丝点击