Insert a node at a specific position in a linked list

  • + 0 comments
    def insertNodeAtPosition(llist, data, position):
        # Write your code here
        counter = 0
        root = SinglyLinkedList()
       
        next_node = llist
        while ( next_node is not None):
    
            if (counter == position):
                root.insert_node(data)
            else:
                temp = next_node
                root.insert_node(temp.data)
                next_node = temp.next
            counter += 1
        return root.head