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();