Insert a node at a specific position in a linked list

  • + 2 comments

    Thanks your explanation cleared my doubt about how the test case is working.

    Here is the python version of it if anyone is interested.

    def InsertNth(head, data, position):
        if not position == 0:
            _head = head
            current_position = 1
            while(position - current_position>0):
                _head = _head.next
                current_position+=1
            if _head.next is None:
                _head.next = Node(data,None)
                return head
            else:
                prev = _head.next
                _head.next = Node(data,prev)
                return head
        else:
            return Node(data,head)