Java Utility Library
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Java.util.LinkedList Class



Java LinkedList Class

Java.util package provides a LinkedList class which has Doubly-linked list implementation of the List and Deque interfaces. The class has all optional list operations, and permits all elements (including null). Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

Class declaration

The declaration of java.util.LinkedList class is:

public class LinkedList<E>
  extends AbstractSequentialList<E>
    implements List<E>, Deque<E>, Cloneable, Serializable

Here, E is the type of element maintained by the container.

Class Constructors

S.NConstructors & Description
1. LinkedList()
Creates an empty List.
2. LinkedList(Collection<? extends E> c)
Creates a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

java.util.LinkedList Methods

The java.util.LinkedList class has a number of methods which are listed below:

Member Methods

S.NMethods & Description
1. boolean add(E element)
Appends the specified element to the end of the list.
2. void add(int index, E element)
Inserts the specified element at the specified position in the list.
3. boolean addAll(Collection<? extends E> c)
Appends all elements of the specified collection at the end of the list.
4. boolean addAll(int index, Collection<? extends E> c)
Inserts all elements of the specified collection at the specified position in the list.
5. void addFirst(E element)
Inserts the specified element at the beginning of the list.
6. void addLast(E element)
Appends the specified element to the end of the list.
7. void clear()
Clears all elements of the list.
8. Object clone()
Returns a shallow copy of the LinkedList.
9. boolean contains(Object obj)
Returns true if the list contains the specified element.
10. Iterator<E> descendingIterator()
Returns an iterator over the elements in the list in reverse sequential order.
11. E element()
Retrieves, but does not remove, the head (first element) of the list.
12. E get(int index)
Returns the element at the specified position in the list.
13. E getFirst()
Returns the first element of the list.
14. E getLast()
Returns the last element of the list.
15. int indexOf(Object obj)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
16. int lastIndexOf(Object obj)
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
17. ListIterator<E> listIterator()
Returns a list iterator over the elements in the list (in proper sequence).
18. boolean offer(E element)
Adds the specified element to the end of the list.
19. boolean offerFirst(E element)
Inserts the specified element at the front of the list.
20. boolean offerLast(E element)
Inserts the specified element to the end of the list.
21. E peek()
Retrieves, but does not remove, the head (first element) of the list.
22. E peekFirst()
Retrieves, but does not remove, the head (first element) of the list, or returns null if the list is empty.
23. E peekLast()
Retrieves, but does not remove, the last element of the list, or returns null if the list is empty.
24. E poll()
Retrieves and removes the head (first element) of the list.
25. E pollFirst()
Retrieves and removes the head (first element) of the list, or returns null if the list is empty.
26. E pollLast()
Retrieves and removes the last element of the list, or returns null if the list is empty.
27. E pop()
Pops the first element of the list.
28. void push(E element)
Inserts the specified element at the front of the list.
29. E remove()
Retrieves and removes the first element of the list.
30. E remove(int index)
Removes the element at the specified position in the list.
31. boolean remove(Object obj)
Removes the first occurrence of the specified element from the list, if it is present.
32. E removeFirst()
Removes and returns the first element from the list.
33. boolean removeFirstOccurrence(Object obj)
Removes the first occurrence of the specified element in this list.
34. E removeLast()
Removes and returns the last element from the list.
35. boolean removeLastOccurrence(Object obj)
Removes the last occurrence of the specified element in this list.
36. E set(int index, E element)
Replaces the element at the specified position in this list with the specified element.
37. int size()
Returns the number of elements in the list.
38. Spliterator<E> spliterator()
Creates a late-binding and fail-fast spliterator over the elements in the list.
39. Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
40. <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array.

Methods inherited

This class inherits the methods of following class:

  • java.lang.Object
  • java.util.AbstractCollection<E>
  • java.util.AbstractList<E>
  • java.util.AbstractSequentialList<E>

5