Linked List

Java LinkedList class uses doubly linked list to store the elements. It inherits the AbstractList class and implements List and Deque interfaces.

  • In java linkedlist class contain duplicate elements,it maintains insertion order and non synchronized.
  • Manipulation is fast  because no shifting need.

Hierarchy of LinkedList class:

 LinkedList class extends AbstractSequentialList class and implements List and Deque interfaces.

Doubly Linked List:

In this list we can add or remove elements from both side.

Class declaration​:

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

Constructors:

Constructors Description
LinkedList() It is used to construct an empty list.
LinkedList(Collection c) It is used to construct a list containing the elements of the specified collection, in the order they are returned by the collection's iterato

 Methods:

  • void add(int index, Object element) - used to add element at specific index in list.
  • void addFirst(Object o) - to insert element at beginning.
  • void addLast(Object o) - to insert element at last.
  • int size() - to find number of element in list.
  • boolean add(Object o) - to append the specified element to the end of a list.
  • boolean contains(Object o) - to return true if the list contains a specified element.
  • boolean remove(Object o) - to remove the first occurence of the specified element in a list.
  • Object getFirst() - to return the first element in a list.
  • Object getLast() - to return the last element in a list.
  • int indexOf(Object o) - to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.
  • int lastIndexOf(Object o) - to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.
Posted on by