Java LinkedList

LinkedList Java

The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class is not synchronized in the linked list, and there is no need for shifting in the linked list, so manipulation is faster. It is used as the list, stack, and queue. In the linked list, doubly linked list means we can insert or delete from both sides of the list. It is the collection of nodes and every node consist of three parts.

  1. One part of the data.
  2. The second part to reference the next node.
  3. And third part to reference the previous node.

Declaration of Linked List

The declaration of the linked list is the same as the array list declaration. We can use a generic collection to declare the linked list.

li- it holds the reference of objects.

The constructor of Linked List

There are two types of Constructor in the linked list:

  1. LinkedList (): This is used to make an empty linked list.
  2. LinkedList (Collection c): This is used to create an ordered list and contains all elements of specified collections.

Methods of Linked List

  1. addFirst (Element e): This method is used to insert the element in the starting of the list.
  2. addLast (Element e): This method is used to insert the element in the last of the list.
  3. addAll (Collection c): It is used to insert all elements at the end of the list.
  4. addAll (int index, Collection c): It is used to insert all elements from the specific index in the list.
  5. add (Element e): It is used to insert the element from the end of the list.
  6. add (Int index, element e): This method is used to insert the element from the specific position in the list.
  7. clone (): This method is used to return the same copy of the list.
  8. remove (): This method is used to remove the first element of the list.
  9. remove (int index): It is used to eliminate the element at a specific position from the list.
  10. remove (object o): It is used to delete the specific element from the list, and it only eliminates the first occurrence of the element.
  11. removeFirst (): This method is used to remove and give back the first element of the list.
  12. removeFirstOccurrence (object o): It is used to eliminate the first occurrence of an element from the list.
  13. removeLast (): This method is used to remove and give back the last element of the list.
  14. removeLastOccurrence (): This method is used to remove the last occurrence of an element from the list.
  15. set (int index, element e): This method is used to set the element at the specific position and replaced that element.
  16. get (int index): This method is used to give back the element at the specific index.
  17. getFirst (): This method is used to give back the first element of the list.
  18. getLast (): This method is used to give back the last element of the list.
  19. clear (): This method is used to remove the elements from the list.
  20. contains (): It is used to check the element is in the list or not. It returns true or false.
  21. IndexOf (object o): It is used to give back the index of the element.
  22. lastIndexOf (object o): It is used to give back the last index of the element.
  23. Size (): This method is used to give the size of the list.

Example of linked list using iterator:

Output

Reverse a list of elements

Output

ListIterator Example

Output

Forward iteration:

Backward iteration:

Example of using different adds methods in Linked List:

Output

Initial list of elements: []

Examples of using set and get methods in the Linked List:

And Output is: