Java LinkedList:

Java LinkedList:
  • Similar to arrays in Java, LinkedList is a linear data structure.
  • In a linked list, the elements are not stored at contiguous memory locations. 
  • Each element in the LinkedList is called the Node. 
  • Each node of the LinkedList contains two items: 
  1. Content of the element
  2. Pointer/Address/Reference to the Next Node in the LinkedList.

Types of Linked List:

  • Simple linked list − Item navigation is forward only.
  • Doubly linked list − Items can be navigated forward and backward.
  • Circular single linked list − Last item contains a link of the first element as next and the first element has a link to the last element as previous.

  • Circular doubly linked list − Items can be navigated forward and backward in circle.
Syntax:
java.lang.Object
     java.util.AbstractCollection
         java.util.AbstractList
            java.util.AbstractSequentialList
                java.util.LinkedList
  • Note: E - the type of elements held in this collection
Why do we need a Linked List?
  • Java LinkedList class uses a doubly linked list to store the elements. 
  • It provides a linked-list data structure. 
  • It inherits the AbstractList class and implements List and Deque interfaces.
  • Linked list allows dynamic memory allocation.
  • Linked list elements don’t need contiguous memory locations.
Hierarchy of LinkedList class in Java:

How to create a linked list in Java?
  • Syntax: 
                      LinkedList object-name= new LinkedList();
  • Example:
                      LinkedList list = new LinkedList();
  • Constructors:
    • LinkedList() : This constructs constructs an empty list.
    • LinkedList(Collection c): This constructs a list containing the elements of the specified collection, in the order, they are returned by the collection's iterator.
Methods:
  • How to add item in the linked list at the end?
    • boolean add(E e): This method appends the specified element to the end of this list.
    • Syntax: public boolean add(E e)
    • Parameters: e − element to be appended
    • Return Value: This method returns true.
    • Example:
list.add("Hello");
list.add(2);
list.add("Chocolate");
list.add("10");
  • How to print the linked list?
    • Example: System.out.println("LinkedList:" + list);
  • How to insert an item at a specific position?
    • void add(int index, E element): This method inserts the specified element at the specified position in this list.
    • Syntax: public void add(int index,E element)
    • Parameters
      • element − index at which the specified element is to be inserted
      • index − element to be appended
    • Return Value: This method does not return a value.
    • Exception: IndexOutOfBoundsException − if the index is out of range
    • Example: list.add(2,"Element");
  • How to insert a collection into a linked list?
    • boolean addAll(Collection c): This method appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
    • Syntax: public boolean addAll(Collection c)
    • Parameters: c − collection containing elements to be added to this list
    • Return Value: This method returns true if this list changed as a result of the call
    • Exception: NullPointerException − if the specified collection is null
    • Example:
Step 01: create a new collection and add some elements
Collection collection = new ArrayList();
collection.add("One");
collection.add("Two");
collection.add("Three");
Step 02: append the collection in the LinkedList
list.addAll(collection);
  • How to add new item at first position?
    • void addFirst(E e): This method returns inserts the specified element at the beginning of this list.
    • Syntax: public void addFirst(E e)
    • Parameters: e − the element to add
    • Return Value: This method does not return any value.
    • Example:
list.addFirst("Add Element");
  • How to add new items at last position?
    • void addLast(E e) : This method returns appends the specified element to the end of this list.
    • Syntax: public void addLast(E e)
    • Parameters: e − the element to add
    • Return Value: This method does not return any value.
    • Example:
list.addLast("Element");
  • How to clear all items from the linked list?
    • void clear(): This method removes all of the elements from this list.
    • Return Value: This method does not return any value.
    • Example: 
list.clear();
  • How to check items in a linked list?
    • boolean contains(Object o) : This method returns true if this list contains the specified element.
    • Syntax: public boolean contains(Object o)
    • Parameters: o − element whose presence in this list is to be tested
    • Return Value: This method returns true if this list contains the specified element
    • Example: System.out.println("List contains 'Sarthak':" + list.contains("Sarthak"));
  • How to get head item from the linked list?
    • E element(): This method retrieves, but does not remove, the head (first element) of this list.
    • Return Value : This method returns the head of this list
    • Exception : NoSuchElementException − if this list is empty
    • Example: System.out.println("Head of list:" + list.element());
  • How to get a specific position item from the linked list?
    • E get(int index): This method returns the element at the specified position in this list.
    • Syntax: public E get(int index)
    • Parameters: index − index of the element to return
    • Return Value: This method returns the element at the specified position in this list.
    • Exception : IndexOutOfBoundsException − if the index is out of range
    • Example: System.out.println("Element at index 3 :" + list.get(3));
  • How to get first position item from the linked list?
    • E getFirst(): This method returns the first element in this list.
    • Syntax: public E getFirst()
    • Return Value : This method returns the first element in this list
    • Exception : NoSuchElementException − if this list is empty
    • Example : System.out.println("First Element :" + list.getFirst());
  • How to get the last position item from the linked list?
    • E getLast(): This method returns the last element in this list.
    • Syntax: public E getLast()
    • Return Value : This method returns the last element in this list
    • Exception : NoSuchElementException − if this list is empty
    • Example :System.out.println("Last Element :" + getLast());
  • How to remove head item from the linked list?
    • E remove(): This method retrieves and removes the head (first element) of this list.
    • Syntax: public E remove()
    • Return Value: This method returns the head of this list
    • Example: list.remove();
  • How to remove specific position item from the linked list?
    • E remove(int index): This method removes the element at the specified position in this list.
    • Syntax: public E remove(int index)
    • Parameters : index − the index of the element to be removed
    • Return Value: This method returns the element previously at the specified position
    • Exception: IndexOutOfBoundsException − if the index is out of range
    • Example: System.out.println("Element to be removed:" + list.remove(2));
  • How to remove the first position item from the linked list?
    • E removeFirst(): This method removes and returns the first element from this list.
    • Syntax: public E removeFirst()
    • Return Value : This method returns the first element from this list
    • Exception : NoSuchElementException − if this list is empty
    • Example :System.out.println("First element:" + list.removeFirst());
  • How to remove the last position item from the linked list?
    • E removeLast(): This method removes and returns the last element from this list.
    • Syntax: public E removeLast()
    • Return Value : This method returns the last element from this list
    • Exception : NoSuchElementException − if this list is empty
    • Example :System.out.println("Last element:" + list.removeLast());
  • How to get the size of the linked list?
    • int size(): This method returns the number of elements in this list.
    • Syntax: public int size()
    • Return Value : This method returns the number of elements in this list
    • Example: System.out.println("List size:" + list.size());

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form