Python code in one pass


  • 0
    K
    def removeNthFromEnd(self, head, n):
            node = head;
            i = 1
            d={}
            while node.next!=None:
                d[i] = node
                i +=1
                node = node.next
            d[i] = node
            length = max(d.keys())
            if n ==1:
                if length >1:
                    d[length-1].next = None
                    return d[1]
                else:
                    return None
            elif n == length:
                return d[2]
            else:
                d[length-n].next = d[length-n+2]
                return d[1]

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.