LinkedList Methods Flashcards
Declaring a LinkedList
LinkedList linkedlist = new LinkedList();
Adding an Item at first of the LinkedList
Method 1 linkedlist.addfirst(“First-Element”);
Method 2 linkedlist.push(“New-Element”);
Adding an Item at Last of the LinkedList
Method 1 linkedlist.addlast(“Last-Element”);
Method 2 linkedlist.add(“Last-Element”);
Adding an element at a particular position of LinkedList
linkedlist.add(3,Element);
Removing the first Item from the LinkedList
Method 1 linkedlist.removefirst();
Method 2 linkedlist.pop();
Method 3 linkedlist.poll();
Method 4 linkedlist.pollfirst();
Removing the last Item from the LinkedList
Method 1 linkedlist.removelast();
Method 2 linkedlist.polllast();
Removing an Item from the LinkedList (Any Element)
linkedlist.remove(“Element”);
Removing an Item from a particular position of an LinkedList
linkedlist.remove(2);
Retrieving an Item from an LinkedList based on the Index
Object X = linkedlist.get(5);
Retrieving first Item from an LinkedList
Method 1 Object FirstElement = linkedlist.getfirst();
Method 2 Object FirstElement = linkedlist.peek();
Method 3 Object FirstElement = linkedlist.peekfirst();
Retrieving last Item from an LinkedList
Method 1 Object LastElement = linkedlist.getlast();
Method 2 Object LastElement = linkedlist.peeklast();
Retrieving first occurrence of Item from an LinkedList - Returns Index
linkedlist.indexof(“Element”);
Retrieving last occurrence of Item from an LinkedList - Returns Index
linkedlist.lastindexof(“Element”);
Updating the value of the element at a particular index in an LinkedList
linkedlist.set(2,”New-Value-Of-The-Element”)
Size of an LinkedList
linkedlist.size();
Duplicate a linkedlist to another linkedlist
DuplicateLinkedlist = linkedlist.clone();
Iterating over an LinkedList (Forward) (Method 1: For Loop)
Method 1 - FOR LOOP
for (int counter = 0; counter < linkedlist.size(); counter++) {
System.out.println(arraylist.get(counter));
Iterating over an LinkedList (Forward) (Method 2: While Loop)
Method 2 - WHILE LOOP int count = 0; while (linkedlist.size() > count) { System.out.println(arraylist.get(count)); count++; }
Iterating over an LinkedList (Forward) (Method 3: ITERATOR )
Method 3 - ITERATOR
Iterator iterator = linkedlist.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
Iterating over an LinkedList (Backward)
BACKWARD ITERATING
ListIterator iterator = linkedlist.iterator();
while (iterator.hasPrevious()) {
System.out.println(iterator.previous());
Is this Item there in the LinkedList
linkedlist.contains(“Element”);
Difference Between ArrayList And LinkedList
ArrayList and LinkedList both implements List interface and their methods and results are almost identical. However there are few differences between them which make one better over another depending on the requirement. ArrayList Vs LinkedList
Search: ArrayList search operation is pretty fast compared to the LinkedList search operation. get(int index) in ArrayList gives the performance of O(1) while LinkedList performance is O(n).Reason: ArrayList maintains index based system for its elements as it uses array data structure implicitly which makes it faster for searching an element in the list. On the other side LinkedList implements doubly linked list which requires the traversal through all the elements for searching an element. Deletion: LinkedList remove operation gives O(1) performance while ArrayList gives variable performance: O(n) in worst case (while removing first element) and O(1) in best case (While removing last element).Conclusion: LinkedList element deletion is faster compared to ArrayList.Reason: LinkedList’s each element maintains two pointers (addresses) which points to the both neighbor elements in the list. Hence removal only requires change in the pointer location in the two neighbor nodes (elements) of the node which is going to be removed. While In ArrayList all the elements need to be shifted to fill out the space created by removed element. Inserts Performance: LinkedList add method gives O(1) performance while ArrayList gives O(n) in worst case. Reason is same as explained for remove. Memory Overhead: ArrayList maintains indexes and element data while LinkedList maintains element data and two pointers for neighbor nodes hence the memory consumption is high in LinkedList comparatively.
Similarities between ArrayList and LinkedList
Both ArrayList and LinkedList are implementation of List interface.
They both maintain the elements insertion order which means while displaying ArrayList and LinkedList elements the result set would be having the same order in which the elements got inserted into the List.
Both these classes are non-synchronized and can be made synchronized explicitly by using Collections.synchronizedList method.
The iterator and listIterator returned by these classes are fail-fast (if list is structurally modified at any time after the iterator is created, in any way except through the iterator’s own remove or add methods, the iterator will throw a ConcurrentModificationException).