Java LinkedList tutorial with examples will help you understand how to use Java LinkedList in an easy way. LinkedList in Java is a doubly linked list implementation of the List and Deque interfaces. The LinkedList class implements all the optional operations defined by the List and allows all elements to be added to it including null elements.
The LinkedList in Java behaves exactly like what is expected from the doubly linked list.
Get operation using the index will fetch the element either from the start of the linked list or from the end of the linked list depending upon which is closer to the specified index.
The LinkedList in Java is not a synchronized implementation. That means if there are multiple threads trying to modify the LinkedList object simultaneously, the code must take care of the synchronization explicitly. That can be done either by synchronizing the linked list object or getting the synchronized list object from the existing LinkedList object using synchronizedList
method of the Collections class as given below.
1 |
List list = Collections.synchronizedList(existingLinkedListObject); |
The Iterator and ListIterator objects can be obtained from the LinkedList in order to iterate over the elements of the LinkedList. The Iterator and ListIterator objects returned by the LinkedList class are fail-fast in nature. That means if the LinkedList object is modified structurally (except through add
or remove
methods of the Iterator) after the Iterator or ListIterator is obtained from the LinkedList, it will throw ConcurrentModificationException
.
Below given are some of the important methods of the LinkedList class.
How to add elements to the LinkedList?
The LinkedList class provides several methods to add elements to the LinkedList object as given below.
How to add an element to LinkedList using the add method?
The add
method appends the given element at the end of the linked list. This method returns true if the element is appended at the end of the LinkedList.
1 2 3 4 5 6 7 8 |
LinkedList<String> linkedList = new LinkedList<String>(); //add elements to linked list object linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); System.out.println("LinkedList elements: " + linkedList); |
Output
1 |
LinkedList elements: [One, Two, Three] |
How to insert an element at the specified index of the LinkedList using the add method?
The overloaded add method inserts the given element at the specified index of the LinkedList object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); System.out.println("LinkedList elements: " + linkedList); /* * insert elements using add method with * the index parameter */ //this will insert element "Five" at index 1 linkedList.add(1, new String("Five")); System.out.println("LinkedList elements: " + linkedList); |
Output
1 2 |
LinkedList elements: [One, Two, Three] LinkedList elements: [One, Five, Two, Three] |
How to add an element at the beginning of the LinkedList using the addFirst method?
The addFirst
method adds an element at the beginning of the LinkedList object (at the front of the LinkedList or at the start of the LinkedList).
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); System.out.println("LinkedList elements: " + linkedList); //this will add element "Five" at the beginning linkedList.addFirst(new String("Five")); System.out.println("LinkedList elements: " + linkedList); |
Output
1 2 |
LinkedList elements: [One, Two, Three] LinkedList elements: [Five, One, Two, Three] |
You can also use the add
method with the index as 0 to add an element at the front of the LinkedList instead of using addFirst
method.
1 |
linkedList.add(0, new String("Five")); |
How to append an element at the end of the LinkedList using the addLast method?
The addLast
method appends an element at the end of the LinkedList object. This method is similar to the add
method as both the methods add the specified element at the end.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); System.out.println("LinkedList elements: " + linkedList); //this will add element "Five" at the end linkedList.addLast(new String("Five")); System.out.println("LinkedList elements: " + linkedList); |
Output
1 2 |
LinkedList elements: [One, Two, Three] LinkedList elements: [One, Two, Three, Five] |
How to add all elements of another list to LinkedList using the addAll method?
The addAll
method of the LinkedList class appends all elements of the specified Collection to the LinkedList at the end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); System.out.println("LinkedList elements: " + linkedList); //ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); aListNumbers.add( new String("Four") ); aListNumbers.add( new String("Five") ); aListNumbers.add( new String("Six") ); /* * Add all elements of the ArrayList to * the LinkedList using addAll method */ linkedList.addAll(aListNumbers); System.out.println("LinkedList elements: " + linkedList); |
Output
1 2 |
LinkedList elements: [One, Two, Three] LinkedList elements: [One, Two, Three, Four, Five, Six] |
The above method adds all the elements of the Collection at the end of the LinkedList. If you want to insert all the elements at the given index instead of at the end, use the overloaded addAll
method with the index parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); System.out.println("LinkedList elements: " + linkedList); //ArrayList ArrayList<String> aListNumbers = new ArrayList<String>(); aListNumbers.add( new String("Four") ); aListNumbers.add( new String("Five") ); aListNumbers.add( new String("Six") ); /* * this will insert all the ArrayList elements * at index 2 of the LinkedList */ linkedList.addAll(2, aListNumbers); System.out.println("LinkedList elements: " + linkedList); |
Output
1 2 |
LinkedList elements: [One, Two, Three] LinkedList elements: [One, Two, Four, Five, Six, Three] |
How to get elements from the LinkedList?
There are several ways to get the elements from the LinkedList.
How to get an element from LinkedList the get method?
To get an element at the specified index from the LinkedList, use the get
method.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); /* * To get the element at specified index * from LinkedList, use get method */ //this will fetch element at index 2 System.out.println( linkedList.get(2) ); |
Output
1 |
Three |
How to get first and last elements from LinkedList using the getFirst and getLast methods?
To get the first element from the LinkedList, use the getFirst
method. Similarly, use the getLast
method to get the last element of the LinkedList.
1 2 3 4 5 6 7 8 9 10 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); //this will get the first element from the LinkedList System.out.println( linkedList.getFirst() ); //this will get the last element from the LinkedList System.out.println( linkedList.getLast() ); |
Output
1 2 |
One Three |
How to get first and last elements from LinkedList using the peek, peekFirst and peekLast methods?
Instead of using getFirst
and getLast
methods, you can also use peek
, peekFirst
and peekLast
methods to get respective elements from the LinkedList.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); //this will get the first element from the LinkedList System.out.println(linkedList.peek() ); //this will also get the first element System.out.println(linkedList.peekFirst() ); //this will get last element of the LinkedList System.out.println( linkedList.peekLast() ); |
Output
1 2 3 |
One One Three |
How to remove elements from the LinkedList?
There are several ways to remove elements from the LinkedList.
How to remove an element at the specified index of LinkedList using the remove method?
To remove an element from the LinkedList at the specified index, use the remove
method with an index argument. The remove
method returns the value which was removed from the LinkedList.
1 2 3 4 5 6 7 8 9 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); //this will remove element at index 1 System.out.println( linkedList.remove(1) ); System.out.println("linked list contains: " + linkedList); |
Output
1 2 |
Two linked list contains: [One, Three] |
How to remove first and last elements from LinkedList using the removeFirst and removeLast methods?
You can use the remove
method without any arguments to remove the first element from the LinkedList. This method throws NoSuchElementException
if the linked list is empty. Similarly, you can use removeFirst
and removeLast
methods to remove the first and the last element from the linked list respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); /* * this will remove first element. The * removeFirst() is same as remove() method. */ System.out.println( linkedList.removeFirst() ); System.out.println("LinkedList contains: " + linkedList); //this will remove last element System.out.println( linkedList.removeLast() ); System.out.println("LinkedList contains: " + linkedList); |
Output
1 2 3 4 |
One LinkedList contains: [Two, Three] Three LinkedList contains: [Two] |
You can also use poll
, pollFirst
and pollLast
methods instead of remove
, removeFirst
and removeLast
methods.
How to remove a specified element from the LinkedList without using the index?
Instead of specifying an index, you can also specify the object or an element you want to remove. To do that, use the remove
method with the Object parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); linkedList.add( new String("Two") ); linkedList.add( new String("Four") ); /* * this will remove the FIRST "Two" from the linked list, * returns true if the element is found and removed. */ System.out.println( linkedList.remove("Two") ); //this will return false as linked list does not contain "Five" System.out.println( linkedList.remove("Five") ); System.out.println("Linked list contains: " + linkedList); |
Output
1 2 3 |
true false Linked list contains: [One, Three, Two, Four] |
You can also use the removeFirstOccurrence
method to remove the first occurrence of the specified object in the linked list.
To remove the last occurrence of the specified object, use the removeLastOccurrence
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.add( new String("One") ); linkedList.add( new String("Two") ); linkedList.add( new String("Three") ); linkedList.add( new String("Two") ); linkedList.add( new String("Four") ); /* * this will remove the last "Two" from the linked list, * returns true if the element is found and removed. */ System.out.println( linkedList.removeLastOccurrence("Two") ); System.out.println("Linked list contains: " + linkedList); |
Output
1 2 |
true Linked list contains: [One, Two, Three, Four] |
How to remove all elements from the LinkedList using the clear method?
The clear
method of the LinkedList class removes all the elements from the linked list object.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); /* * The clear method removes all elements * from the LinkedList */ linkedList.clear(); System.out.println("Linked list size: " + linkedList.size()); |
Output
1 |
Linked list size: 0 |
How to get the size of the LinkedList using the size method?
To get the number of elements stored in the LinkedList, use the size
method.
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedList = new LinkedList<String>(); //this will return 0 as linked list empty System.out.println("LinkedList size: " + linkedList.size()); //add elements to linked list object linkedList.add( new String("One") ); linkedList.add( new String("Two") ); //this will return 2 System.out.println("LinkedList size: " + linkedList.size()); |
Output
1 2 |
LinkedList size: 0 LinkedList size: 2 |
How to check if the LinkedList is empty using the isEmpty method?
The isEmpty
method of the LinkedList returns true if the linked list is empty (i.e. there are no elements in the linked list). It returns false if there is at least one element in the LinkedList. Alternatively, you can also get the linked list size using the size
method and compare it with zero to check if the linked list is empty.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
LinkedList<String> linkedList = new LinkedList<String>(); //this will return true as linked list empty System.out.println("Is linked list empty: " + linkedList.isEmpty()); //or using size method System.out.println("Is linked list empty: " + (linkedList.size() == 0) ); //add element to linked list object linkedList.add( new String("One") ); //this will return false as there is one element in the linked list System.out.println("Is linked list empty: " + linkedList.isEmpty()); |
Output
1 2 3 |
Is linked list empty: true Is linked list empty: true Is linked list empty: false |
How to replace an element in the LinkedList using the set method?
The set
method replaces an element with the given element at the specified index of the linked list. The set
method returns the element which was replaced by the specified new element at the given index.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); /* * Use set method to replace an element. * It returns the element that was replaced. */ //this will replace Three with Five, return Three System.out.println( linkedList.set(2, "Five") ); System.out.println("Linked list contains: " + linkedList); |
Output
1 2 |
Three Linked list contains: [One, Two, Five] |
How to convert LinkedList to an array using the toArray method?
The toArray
method of the LinkedList class converts the linked list object to an array. The array contains all the elements of the LinkedList in the same order.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); //create an array with the size equal to linked list size String[] stringArray = new String[ linkedList.size() ]; //use toArray method to convert linkedList.toArray(stringArray); System.out.println("Array contains: " + Arrays.toString(stringArray)); |
Output
1 |
Array contains: [One, Two, Three] |
How to iterate LinkedList?
There are several ways using which you can iterate the linked list object.
How to iterate LinkedList using the Iterator?
1 2 3 4 5 6 7 8 9 10 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); //get the iterator Iterator<String> iterator = linkedList.iterator(); while( iterator.hasNext() ){ System.out.println( iterator.next() ); } |
Output
1 2 3 |
One Two Three |
How to iterate LinkedList using ListIterator?
1 2 3 4 5 6 7 8 9 10 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); //get the ListIterator ListIterator<String> listIterator = linkedList.listIterator(); while( listIterator.hasNext() ){ System.out.println( listIterator.next() ); } |
Output
1 2 3 |
One Two Three |
Additionally, you can also iterate the linked list in the reverse direction using a ListIterator object.
1 2 3 4 5 6 7 8 9 10 11 12 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); //specify the linked list size as the starting point (i.e. the end of linked list) ListIterator<String> reverseListIterator = linkedList.listIterator( linkedList.size() ); //iterate in backward direction using hasPrevious and previous methods while(reverseListIterator.hasPrevious()){ System.out.println( reverseListIterator.previous() ); } |
Output
1 2 3 |
Three Two One |
How to iterate LinkedList using enhanced for loop?
1 2 3 4 5 6 7 8 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); for(String string : linkedList){ System.out.println(string); } |
Output
1 2 3 |
One Two Three |
How to iterate LinkedList in reverse direction using the descendingIterator method?
The descendingIterator
method returns an iterator over the elements of the linked list in the reverse order (i.e. starting from the end to the start of the linked list).
1 2 3 4 5 6 7 8 9 10 11 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); //get the descending iterator Iterator<String> reverseIterator = linkedList.descendingIterator(); while(reverseIterator.hasNext()){ System.out.println( reverseIterator.next() ); } |
Output
1 2 3 |
Three Two One |
How to get a sublist from LinkedList using the subList method?
The subList
method returns a portion of the LinkedList between the specified start and end index. The start index is inclusive while the end index is exclusive. The returned List object is backed by the original linked list, so any changes you make to the sublist will also be reflected back to the linked list, and vice versa.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
LinkedList<String> linkedList = new LinkedList<String>(); linkedList.addLast(new String("One")); linkedList.addLast(new String("Two")); linkedList.addLast(new String("Three")); linkedList.addLast(new String("Four")); linkedList.addLast(new String("Five")); /* * subList returns a view of a portion * of the linked list */ //this will return a list containing Two and Three List<String> subList = linkedList.subList(1, 3); System.out.println("sub list contains:" + subList); /* * Any change you make in the sublist will * be reflected back to the linked list, and vice versa */ subList.set(0, "SubListTwo"); System.out.println("Linked list contains:" + linkedList); linkedList.set(2, "LinkedListThree"); System.out.println("Sub list contains:" + subList); |
Output
1 2 3 |
sub list contains:[Two, Three] Linked list contains:[One, SubListTwo, Three, Four, Five] Sub list contains:[SubListTwo, LinkedListThree] |
The below given additional Java LinkedList examples will help you understand the linked list in more detail.
Java LinkedList Examples
- How to iterate through LinkedList
- How to iterate through LinkedList in reverse order or backward direction
- How to convert LinkedList to String
- How to convert HashMap to LinkedList
- Convert LinkedList to an Array
- Convert array to LinkedList
- Add an Array to Java LinkedList
- Remove duplicate elements from Java LinkedList
- How to get elements from Java LinkedList
- How to get first and last elements of Java LinkedList
- How to add elements to Java LinkedList
- Iterate Java LinkedList in reverse direction (backward)
- How to replace elements of Java LinkedList
- How to remove elements of Java LinkedList
- Find index of LinkedList elements using indexOf and lastIndexOf methods
- Check if element exists in Java LinkedList (LinkedList contains element)
- Sort Java LinkedList example (using Comparator and Comparable)
- How to remove the last element of the Java LinkedList
- How to clear Java LinkedList (empty LinkedList)
- How to check if the LinkedList is empty in Java
- How to get Java LinkedList size
- How to add an element at the beginning of the Java LinkedList
- How to print Java LinkedList elements
References:
Java 8 LinkedList
Please let me know if you liked the Java LinkedList tutorial with examples in the comments section below.