Insert a node at a specific position in a linked list

  • + 3 comments

    May be test cases are changed, following code passes all test cases:

    def InsertNth(head, data, position):
        if position == 0:
            head = Node(data, head)
        else:
            cur = head
            for _ in range(position-1):
                cur = cur.next
    
            cur.next = Node(data, cur.next)
    
        return head