Java Program for deletion in singly linked list

Singly linked list Deletion

Java Program for deletion in singly linked list. In the singly linked list we can delete the node in the following ways or we can say they ways of deleting nodes .When we delete the node in the linked list then there are  three ways to delete the node as follows.

  • Deletion at beginning.
  • Deletion at middle or n node.
  • Deletion at last.

Java Program for deletion in singly linked list. It is the Deletion of nodes which is the simplest operation. This all needs just a few adjustment in nodes pointers. It is also a Efficient memory utilization,i.e no need to pre-allocate memory. Linear data like a stack, a queue can be easily executed using linked list.

Java Program for deletion in the singly linked list

 Deletion in the beginning in singly linked list

In this deletion we will delete the very first node we have in our diagram.

Algorithm.

step 1- Assign a head node

step 2- Assign a new starting node as head node.

Step 3- Now delete the  first node.

Java Program for deletion at the beginning in singly linked list

Deletion in the middle in singly linked list

Here we will pick a node from middle then we will delete it.

Algorithm.

Step 1- Search the node you want to delete and name it temp.

step 2- Store previous node and call it previous

Step 3- Assign previous ->next to temp ->next

Step 4- delete your node

Java Program for deletion at the middle in singly linked list

Deletion in the end in singly linked list

Over we will delete the last node in our diagram.

Algorithm.

Step 1- For deleting the last node we have to traverse the list from head node and continue traverse until the address part of the node becomes null.

Step 2- Store the previous and call it previous node.

Step 3- When the address part of the node is null set the address part of previous node as null .

Step 4- Delete the node.

 

Java Program for deletion at the end in singly linked list

Java Program for deletion in singly linked list

// A complete working Java program to delete a node in a linked list 
// at a given position 
class PrepInsta
{ 
	Node head; // head of list 

	// Linked list Node
	class Node 
	{ 
		int data; 
		Node next; 
		Node(int d) 
		{ 
			data = d; 
			next = null; 
		} 
	} 

	// Inserts a new Node at front of the list.
	public void push(int new_data) 
	{ 
		// 1 & 2: Allocate the Node & Put in the data
		Node new_node = new Node(new_data); 

		// 3. Make next of new Node as head 
		new_node.next = head; 

		// 4. Move the head to point to new Node 
		head = new_node; 
	} 

	// Given a reference to the head of a list and a position, deletes the node at the given position 
	void deleteNode(int position) 
	{ 
		// If linked list is empty 
		if (head == null) 
			return; 

		// Store head node 
		Node temp = head; 

		// If head needs to be removed 
		if (position == 0) 
		{ 
			head = temp.next; // Change head 
			return; 
		} 

		// Find previous node of the node to be deleted 
		for (int i=0; temp!=null && i<position-1; i++) 
			temp = temp.next; 

		// If position is more than number of ndoes 
		if (temp == null || temp.next == null) 
			return; 

		// Node temp->next is the node to be deleted 
		// Store pointer to the next of node to be deleted 
		Node next = temp.next.next; 

		temp.next = next; // Unlink the deleted node from list 
	} 

	// This function prints contents of linked list starting from the given node 
	public void printList() 
	{ 
		Node tnode = head; 
		while (tnode != null) 
		{ 
			System.out.print(tnode.data+" "); 
			tnode = tnode.next; 
		} 
	} 

	// program to test above functions. Ideally this function 
	//   should be in a separate user class. It is kept here to keep code compact 
	public static void main(String[] args) 
	{ 
		// Start with the empty list 
		LinkedList llist = new LinkedList(); 

		llist.push(10); 
		llist.push(20); 
		llist.push(30); 
		llist.push(40); 
		llist.push(50);
		llist.push(60);

		System.out.println("Created Linked list is: "); 
		llist.printList(); 

		llist.deleteNode(10); // Delete node at position 1 
		llist.deleteNode(30); // Delete node at position 3
		llist.deleteNode(10); // Delete node at position 6

		System.out.println("Linked List after Deletion at  position 1: "); 
		llist.printList(); 
			System.out.println("Linked List after Deletion at position 3: "); 
		llist.printList(); 
			System.out.println("Linked List after Deletion at position 6: "); 
		llist.printList(); 
	} 
}
Output
Linked list before operation : 10  20  30  40  50  60
value deleted 10
value deleted 30
value deleted 60
Linked List after operation: 20 40 50