LinkedList - insertion/deletion
by, 11-01-2011 at 06:48 PM (1116 Views)
LinkedList has a benefit over ArrayList/Vector when it comes to insert and delete operation. LinkedLists can add and delete any element in the list very efficiently because only the node pointers are to be updated. While talking about the memory consumption, each element/object in a LinkedList takes a bit more memory as compared to ArrayList/Vector because of the pointers involved.
ArrayList, Vector and LinkedList all have their importance. The important thing is to use them in the right situation.
In the example below, we created a LinkedList and added 2 objects to it. We then added a 3rd object at the first index using addFirst() method. This will add the new node to the first place and only few pointers are readjusted. This operation in LinkedList is very efficient as compared to ArrayList/Vector.
Java Code:LinkedList ll = new LinkedList(); ll.add("Pluto"); ll.add("Neptune"); ll.addFirst("Mercury"); for(int i=0;i System.out.println(ll.get(i));