LeetCode-19-Remove-Nth-Node-From-End-of-List 链表水题

来源:互联网 发布:淘宝欢乐逛 编辑:程序博客网 时间:2024/06/05 09:38




# Definition for singly-linked list.# class ListNode(object):#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution(object):    def removeNthFromEnd(self, head, n):        """        :type head: ListNode        :type n: int        :rtype: ListNode        """        h=head        cnt=1        while h.next!=None:            cnt+=1            h=h.next        p=cnt-n+1        if p==1:return head.next        h=head        cnt2=1        while h.next!=None:            if cnt2==p-1:                h.next=h.next.next                return head            cnt2+=1            h=h.next


阅读全文
1 0
原创粉丝点击