Insert a node at a specific position in a linked list

  • + 0 comments

    simplest python solution:

    def insertNodeAtPosition(llist, data, position):
        new_node = SinglyLinkedListNode(data)
        curr = llist
        prev = llist
            
        for i in range(position):
            prev = curr
            curr = curr.next
            
        prev.next = new_node
        new_node.next = curr
        
        return llist