Prev Next
- LinkedList is a class which implements the List interface.
- It is similar to that of LinkedList except that the ArrayList uses Array data structure implicitly whereas LinkedList uses doubly linked list internally to store the elements. Like ArrayList, LinkedList is also non synchronized.
- Search operation is faster in ArrayList, since the index value is enough to retrieve an element, whereas in LinkedList one has to traverse through all the elements.
- Operations like insert and delete are faster in LinkedList since only the pointer location has to be changed whereas in ArrayList all the elements needed to be shifted after the change.
- Thus LinkedList is better for manipulating the data and ArrayList is better for storing and accessing the data.
Creating a LinkedList:
Syntax:
LinkedList object = new LinkedList(); //Without Generics
LinkedList<DataType> object = new LinkedList<DataType>(); //Using Generics |
Example:
LinkedList<Integer> list_num = new LinkedList<Integer>(); |
Methods of LinkedList | Description |
boolean add(E e) | Appends the element ‘e’ to the end of the list. |
void add(int i, E e) | Inserts the element ‘e’ at the specified index ‘i’ in the list. |
void addFirst(E e) | Inserts the element ‘e’ in the beginning of the list |
void addLast(E e) | Inserts the element ‘e’ in the end of the list |
void clear() | Removes all the elements from the list. |
boolean contains(Object o) | Returns true if the list contains the object specified in the argument. |
E get(int index) | Returns the element at the specified index in the list. |
boolean isEmpty() | Returns true if the list has no elements |
E remove(int index) | Removes the element at the specified index in the list. |
E removeFirst() | Removes and returns the first element in the list. |
boolean removeFirstOccurence(Object o) | Removes the first occurrence of the element in the list. |
E removeLast() | Removes and returns the last element in the list. |
boolean removeLastOccurence(Object o) | Removes the last occurrence of the element in the list (traversing from head to tail) |
E set(int index, E element) | Replaces the element at the specified index in the list with the element given. |
int size() | Returns the number of elements in the list. |
peek() | Retrieves the element in the head, but does not remove. |
E peekFirst() | Retrieves the first element of the list, but does not remove or returns null if this list is empty. |
E peekLast() | Retrieves the last element of the list, but does not remove or returns null if this list is empty. |
E poll() | Retrieves and removes the element in the head. |
E pollFirst() | Retrieves and removes the first element of the list. |
E pollLast() | Retrieves and removes the last element of the list. |
E pop() | Pops an element from the list, considering the list as stack. |
void push(E e) | Inserts the element e to the list, considering the list as stack. |
boolean addAll(Collection<? extends E> c) | Appends all of the elements in the specified collection to the end of the list. |
boolean removeAll(Collection<?> c) | Removes all the elements that are contained in the specified collection from the LinkedList. |
List<E> subList(int from, int to) | Returns a list which contains the elements between the specified positions. from – inclusive , to – exclusive. |
Adding elements in the LinkedList:
Elements can be added to LinkedList object by using add() method of the LinkedList class.
Syntax:
add(E e) |
To Add an element at a specific index.
Syntax:
add(int index, Element e) |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class LinkedListExample{ public static void main(String[] args) { LinkedList<String> Box= new LinkedList<String>(); Box.add("Apple"); Box.add("Mango"); Box.add(1,"Grapes"); //adding “Anu” in the index 1 for(String s:Box){ //iterating using for loop System.out.println("Item:"+s); } } } |
Output:
Item:Apple Item:Grapes Item:Mango |
Removing Element from the LinkedList
Elements can be removed from an LinkedList using remove() method of the LinkedList class.
Syntax:
remove(E e) |
The above method removes the first occurrence of the element ‘e’ from the array list.
Syntax:
remove(int index) |
The above method removes the element from the position (index) specified in the argument.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class LinkedListExample{ public static void main(String[] args) { LinkedList<String> Box= new LinkedList<String>( Arrays.asList(“Apple”, “Grapes”,”Mango”)); //Initialize LinkedList using Arrays.asList() System.out.println(“LinkedList:”+Box); System.out.println("Removing element in the position 2:"); Box.remove(2); System.out.println(“LinkedList:”+Box); System.out.println("Removing element ‘Apple’ from the list:"); Box.remove(“Apple”); System.out.println("After Removal.."); System.out.println(“LinkedList:”+Box); } } |
Output:
LinkedList: [Apple,Grapes,Mango]
Removing element in the position 2: LinkedList: [Apple,Grapes] Removing element ‘Apple’ from the list: |
Finding the Length of the LinkedList:
The length of an LinkedList can be found using size() method of the LinkedList class.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public class LinkedListExample{ public static void main(String[] args) { LinkedList<String> list= new LinkedList<String>( Arrays.asList(“Aparna”, “Ashish”,”Chandini”,”Dinesh”)); System.out.println(“Names:”+list); list.add(“Emi”); System.out.println(“Emi added to the list”); int size = list.size(); System.out.println(“Size after adding an element:”+size); } } |
Output:
Names: [Aparna, Ashish,Chandini,Dinesh]
Emi added to the list Size after adding an element:5 |