Linked List Data Structure in Java, easy in 5 minutes

In the Linked List Data Structure in Java tutorial, you will learn how to create and perform different operations on a linked list. You will also learn about its applications. A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image:

Linked List Data Structure in Java

A linked list is a series of connected “nodes” that contains the “address” of the next node. Each node can store a data point which may be a number, a string or any other type of data.

The address of first node in the linkedlist is HEAD. The last node in the linkedlist can be identified because it’s next portion points to NULL.

Why do we need Linked List?

Both of Linked List and Arrays can be used to store linear data structure, but arrays have some limitations:

  1. The size of arrays is fixed and the allocated memory is equal to the upper limit irrespective of the usage. A Linkded List is a dynamic size and the allocated memory is equal to the real size of usage.
  2. Inserting a new element in an array of elements is expensive. But it is ease of insertion/deletion with Linked List.

Drawbacks of Linked List

  1. Random access is not allowed. We must only access nodes sequentially starting from the first node.
  2. Extra memory space for a pointer of each node in the list.
  3. Not cache friendly.
  4. One of the disadvantages of using a linked list to store data is the time necessary to search for an item. Since Linked Lists are linear structures the time required to search a “linear” list is proportional to the size of the data set. This issue is sorted out if we use a nonlinear data structure like a tree data structure.

How another node is referenced in the linkedlist?

A linked list is designed by a pointer to the first node. The first node is called the head. If the linked list is empty, then the value of the head is NULL. Each node in a list consists of at least two parts:

  • A data item
  • Pointer to the next node

Implement Linked List Data Structure in Java

We might implement the above specification of LinkedList in Java language like this:

Here is a simple program that demonstrates a linked list.

Here is output of the above program

That’s all about Linked List Data Structure in Java.

References
Linked List Data Structure in Java
Data Structures and Algorithms Tutorial in Java

Please share it if you found this useful
Hide Buttons