Open In App

Implementing a Linked List in Java using Class

Last Updated : 04 Jan, 2025
Comments
Improve
Suggest changes
73 Likes
Like
Report

Pre-requisite:Linked List Data Structure

Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at the contiguous location, the elements are linked using pointers as shown below. 

linkedlist


Creation and Insertion:

In this article, insertion in the list is done at the end, that is the new node is added after the last node of the given Linked List. For example, if the given Linked List is 5->10->15->20->25 and 30 is to be inserted, then the Linked List becomes 5->10->15->20->25->30. 
Since a Linked List is typically represented by the head pointer of it, it is required to traverse the list till the last node and then change the next to last node to the new node.

linkedlist_insert_last 

Implementation:


Output
LinkedList: 1 2 3 4 5 6 7 8 

Traversal: For traversal, below is a general-purpose function printList() that prints any given list by traversing the list from head node to the last.

Implementation:


Output
LinkedList: 1 2 3 4 5 6 7 8 

Deletion By KEY:

The deletion process can be understood as follows:

To be done: 

Given a 'key', delete the first occurrence of this key in the linked list.

How to do it: 

To delete a node from the linked list, do following steps.  

  1. Search the key for its first occurrence in the list
  2. Now, Any of the 3 conditions can be there: 
    • Case 1: The key is found at thehead 
      1. In this case, Change the head of the node to the next node of the current head. 
         
      2. Free the memory of the replaced head node.
    • Case 2: The key is found in the middle or last, except at thehead 
      1. In this case, Find the previous node of the node to be deleted. 
      2. Change the next the previous node to the next node of the current node.
      3. Free the memory of the replaced node.
    • Case 3: The key is not found in the list 
      1. In this case, No operation needs to be done. 

linkedlist_deletion

Implementation:


Output
LinkedList: 1 2 3 4 5 6 7 8 
1 found and deleted
LinkedList: 2 3 4 5 6 7 8 
4 found and deleted
LinkedList: 2 3 5 6 7 8 
10 not found
LinkedList: 2 3 5 6 7 8 

Deletion At Position:

This deletion process can be understood as follows:

To be done: 
Given a 'position', delete the node at this position from the linked list.
How to do it: 

The steps to do it are as follows: 

  1. Traverse the list by counting the index of the nodes
  2. For each index, match the index to be same as position
  3. Now, Any of the 3 conditions can be there: 
    • Case 1: The position is 0, i.e. the head is to be deleted 
      1. In this case, Change the head of the node to the next node of current head. 
         
      2. Free the memory of replaced head node.
    • Case 2: The position is greater than 0 but less than the size of the list, i.e. in the middle or last, except at head 
      1. In this case, Find previous node of the node to be deleted. 
         
      2. Change the next of previous node to the next node of current node.
      3. Free the memory of replaced node.
    • Case 3: The position is greater than the size of the list, i.e. position not found in the list 
      1. In this case, No operation needs to be done. 

linkedlist_deletion

Implementation:


Output
LinkedList: 1 2 3 4 5 6 7 8 
0 position element deleted
LinkedList: 2 3 4 5 6 7 8 
2 position element deleted
LinkedList: 2 3 5 6 7 8 
10 position element not found
LinkedList: 2 3 5 6 7 8 

All Operations:

Below is the complete program that applies each operation together:


Output
LinkedList: 1 2 3 4 5 6 7 8 

1 found and deleted

LinkedList: 2 3 4 5 6 7 8 

4 found and deleted

LinkedList: 2 3 5 6 7 8 

10 not found

LinkedList: 2 3 5 6 7 8 

0 position element deleted

LinkedList: 3 5 6 7 8 

2 position element deleted

LinkedList: 3 5 7 8 

10 position element not found

LinkedList: 3 5 7 8 

Next Article

Similar Reads