Java linked list methods

Java LinkedList class has a lot of methods to perform different types of operation. In this post, we will discuss all the Java linked list methods with an example. Let’s see the Java linked list methods.

Java linked list methods

1. How to add elements to a LinkedList?

  1. add(E e): This method adds a node in LinkedList. It appends the given element e at the end of the LinkedList. You can read it with an example.
  2. add(int index, E e): This method adds a node in LinkedList at a specific position. It inserts the element e, at a particular index. You can read it with an example.
  3. addAll(Collection c): The addAll(Collection c) method adds all the elements in LinkedList. It appends the specified collection to the end of this LinkedList. You can read it with an example.
  4. addAll(int index, Collection c): This method adds all the elements of the specified collection at a specific index. You can read it with an example.
  5. addFirst(E e): This method adds the given element e at the beginning of LinkedList. You can read it with an example.
  6. addLast(E e): This method is used to add the given element at the end of the LinkedList. You can read it with an example.
  7. offer(E e): The offer() method also used to add the specified element at the end of the LinkedList. You can read it with an example.
  8. offerFirst(E e): This method is used to add the given element at the beginning of LinkedList. Its return type is boolean. You can read it with an example.
  9. offerLast(E e): This method is used to add the given element at the end of LinkedList. Its return type is boolean. You can read it with an example.
  10. push(): This method is used to add the given element at the beginning of the LinkedList. Its return type is void. You can read it with an example.

2. How to access elements from a LinkedList?

  1. remove(): This method is used removes and returns the first element from LinkedList. Its return type is E. You can read it with an example
  2. remove(Object o): This method is used to remove the first occurrence of the specified element from LinkedList. You can read it with an example
  3. remove(int index): This method is used to remove the element at the specified position from LinkedList. You can read it with an example
  4. removeFirst(): This method is used to remove and return the first element from LinkedList. You can read it with an example
  5. removeLast(): This method is used removes and returns the last element from LinkedList. You can read it with an example
  6. removeFirstOccurrence(): This method is used removes the first occurrence of the specified element from LinkedList. You can read it with an example
  7. removeLastOccurrence(): This method is used removes the last occurrence of the specified element from LinkedList. You can read it with an example
  8. poll(): This method is used to removes and return the first element from LinkedList. You can read it with an example
  9. pollFirst(): This method is also used to removes and return the first element from LinkedList. Its return type is E. You can read it with an example
  10. pollLast(): This method is used to removes and return the last element from LinkedList. Its return type is E. You can read it with an example
  11. pop(): This method is also used to removes and return the first element from LinkedList. Its return type is E. You can read it with an example
  12. removeAll(Collection c): The removeAll(Collection c) method is used to remove all elements of the given collection from LinkedList. It removes all elements from LinkedList whether it is duplicated in LinkedList or not. You can read it with an example
  13. removeIf(Predicate filter) method: The removeIf(Predicate filter) method is also used to remove the element from the LinkedList. It takes an argument of Predicate type. You can read it with an example

3. How to get element in ArrayList java?

  1. get(int index): This method is used to get the element at a specified index in LinkedList. You can read it with an example
  2. getFirst(): This method is used to get the first element from LinkedList. You can read it with an example
  3. getLast(): This method is used to get the last element from LinkedList. You can read it with an example
  4. peek(): This method is used to get to the first element from LinkedList. Its return type is E. You can read it with an example
  5. peekFirst(): This method is also used to get the first element from LinkedList. You can read it with an example
  6. peekLast(): This method is used to get to the last element from LinkedList. You can read it with an example
  7. element(): This method is used to get the first element from LinkedList. But doesn’t remove the element from list. Its return type is E. You can read it with an example

4. Searching in linked list

  1. index(object o): This method is used to get the index of the specified element. It returns the index value of the first occurrence of the element if LinkedList. You can read it with an example
  2. lastIndexOf(Object o):  This method is used to get the last index of the specified element. It returns the index value of the last occurrence of the element of LinkedList. You can read it with an example.
  3. contains(Object o): It is used to check whether the given element present in LinkedList or not. Its return type is boolean. You can read it with an example

5. How to perform linked list sorting

  1. sort(Comparator<? super E> c): This method is used to sort the elements of the LinkedList based on a specified comparator. You can read it with an example

6. How to convert linked list to array in java

  1. Object[] toArray(): This method is used to return an array containing all of the elements of the LinkedList in the correct order. You can read it with an example
  2. T [] toArray(T [] O): This method is used to return an array containing all of the elements in the LinkedList in the correct order same as the previous method. You can read it with an example

7. Traverse a linked list java

  1. iterator(): It is used to return an iterator over the elements in this LinkedList. You can read it with an example
  2. Spliterator: It is used to create a late-binding and fail-fast Spliterator over the elements in the LinkedList. You can read it with an example
  3. listIterator​(): It is used to return a list iterator over the elements in LinkedList. You can read it with an example
  4. listIterator​(int index): It is used to return a list iterator over the elements in LinkedList starting at the specified position in the LinkedList. You can read it with an example

These all are the Java linked list methods. You can click on link and read each method with example.

Leave a Comment