When adding an element at a certain index using the add(index, element) method in an ArrayList, it places the element at that index, while all the other elements change their indexes by 1 (they move in memory). That's why an ArrayList has the complexity O(n) when adding an element at a certain position.
In case of a doubly LinkedList, I know that the elements have pointers to the previous element and also for the next one.
My question is, when using the add(index, element) method related to a LinkedList, what will actually happen behind the scenes? I know that using the LinkedList the rest of the elements don't move in memory, so how come they can still be placed at a certain index without moving in memory?
get()method requires a list traversal to access the correct index. This is whyArrayList::gethas a complexity of O(1) (it can just jump to the correct memory address), butLinkedList::gethas a complexity of O(n).