Implementation of LinkedList by using Java Language.

What is LinkedList?

LinkedList is one type of linear data structure which is used to store data. In the linked list elements are not stored in contiguous locations. The elements are linked using pointers and addresses. Every element of the linked list is known as a Node.

In the Node, there are two parts: One part is for data storage. And the second part is for the address of the next Node.

How to Implement Generic LinkedList in Java? - GeeksforGeeks

How does LinkedList work Internally?

LinkedList acts as a dynamic array.

Why do we say dynamic array?

Because we do not need to specify the size while creating LinkedList. The size of the list automatically increases when we insert a new element into the list.

There are some methods used in LinkedList:

Method

Description

add(int index,E element)

This method is used to insert a new element at the specified position in this list.

clear()

This method is used to remove all of the elements from this list.

clone()

This method returns a shallow copy of this LinkedList.

contains(Objects o)

This method returns true if this list contains the specified element.

get(int Index)

This method returns the elements of the specified position in this list.

indexOf(Object o)

This method returns the index of the first occurrence of the specified element in this list.

peek()

This method returns the top element of the list.

push()

This method pushes an element onto the stack represented by this list.

pop()

This method removes the element from the stack represented by this list.

remove()

This method removes and retrieves the head of this list.

size()

This method returns the number of elements in this list.