Algorithms

Delete given node from singly linked list

<p>In this article&comma; we will discuss how to delete a given node from the singly linked list&period; Given a pointer to a node to be deleted but we don’t have a pointer to head node&period; In a singly linked list&comma; each node in the list has the content and a pointer to the next node in the list&period; It does not store any pointer to the previous node&period; To store a single linked list&comma; only the pointer to the first node in that list must be stored&period; The last node in a single linked list points to nothing&period;<&sol;p>&NewLine;<p>In previous articles we have discussed some important algorithm related to the linked list like the following&colon;<&sol;p>&NewLine;<ul>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;nth-node-from-the-end-of-a-singly-linked-list&sol;">Find the nth node from the end of a singly linked list<&sol;a><&sol;strong><&sol;li>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;find-the-middle-element-in-a-linked-list&sol;">Find the middle element in a linked list<&sol;a><&sol;strong><&sol;li>&NewLine;<li><strong><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;reverse-linked-list&sol;">How to Reverse linked list in Java<&sol;a><&sol;strong><&sol;li>&NewLine;<&sol;ul>&NewLine;<p>In this article&comma; we are going to discuss another important algorithm related to the Linked List&period; Let&&num;8217&semi;s see as the following diagram&colon;<br &sol;>&NewLine;<img class&equals;"aligncenter wp-image-4252 size-full" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2018&sol;09&sol;Singly&lowbar;linked&lowbar;list&lowbar;delete&lowbar;after&period;png" alt&equals;"Delete given node from singly linked list" width&equals;"263" height&equals;"130" &sol;>As you can see that we will delete given node from singly linked list&period;<&sol;p>&NewLine;<h2>Delete given node from singly linked list<&sol;h2>&NewLine;<p>There are two approaches to delete given node from singly linked list at the following&colon;<&sol;p>&NewLine;<h3>Approach 1&colon; We have a pointer to head node<&sol;h3>&NewLine;<p>It is a very simple solution to traverse the linked list until you find the node you want to delete&period; But this solution requires the pointer to the head node which contradicts the problem statement&period;<&sol;p>&NewLine;<h3>Approach 2&colon; We don’t have a pointer to head node<&sol;h3>&NewLine;<p>In this case&comma; we can use a fast solution&period; We can copy the data from the next node to the node to be deleted and delete the next node as the following&colon;<&sol;p>&NewLine;<pre> &sol;&sol; Find next node using next pointer &NewLine; Node temp &equals; node&lowbar;ptr->next&semi; &NewLine; &sol;&sol; Copy data of next node to this node &NewLine; node&lowbar;ptr->data &equals; temp->data&semi; &NewLine; &sol;&sol; Unlink next node &NewLine; node&lowbar;ptr->ext &equals; temp->next&semi; &NewLine; &sol;&sol; Delete next node &NewLine; free&lpar;temp&rpar;&semi; &NewLine;<&sol;pre>&NewLine;<p>Let&&num;8217&semi;s see the complete code&colon;<&sol;p>&NewLine;<pre>&sol;&ast;&ast; &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;package com&period;dineshonjava&period;algo&semi; &NewLine; &NewLine;import java&period;util&period;Optional&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh&period;Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class LinkedListTest &lbrace; &NewLine; &NewLine;&Tab;&sol;&ast;&ast; &NewLine;&Tab; &ast; &commat;param args &NewLine;&Tab; &ast;&sol; &NewLine;&Tab;public static void main&lpar;String&lbrack;&rsqb; args&rpar; &lbrace; &NewLine;&Tab;&Tab; &NewLine;&Tab;&Tab;Node listNode &equals; createLinkedList&lpar;9&rpar;&semi; &NewLine;&Tab;&Tab;System&period;out&period;println&lpar;"Before deleting"&rpar;&semi; &NewLine;&Tab;&Tab;printList&lpar;listNode&rpar;&semi; &NewLine; deleteNode&lpar;listNode&period;next&period;next&rpar;&semi; &NewLine; System&period;out&period;println&lpar;"&bsol;nAfter Deleting"&rpar;&semi; &NewLine; printList&lpar;listNode&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&Tab; &NewLine;&Tab;private static Node createLinkedList&lpar;int n&rpar; &lbrace; &NewLine;&Tab; Node head &equals; new Node&lpar;"1"&rpar;&semi; &NewLine;&Tab; Node current &equals; head&semi; &NewLine;&Tab; &NewLine;&Tab; for &lpar;int i &equals; 2&semi; i <&equals; n&semi; i&plus;&plus;&rpar; &lbrace; &NewLine; Node newNode &equals; new Node&lpar;String&period;valueOf&lpar;i&rpar;&rpar;&semi; &NewLine; current&period;setNext&lpar;newNode&rpar;&semi; &NewLine; current &equals; newNode&semi; &NewLine; &rcub; &NewLine; return head&semi; &NewLine; &rcub; &NewLine; &NewLine; public static void deleteNode&lpar;Node Node&lowbar;ptr&rpar; &NewLine; &lbrace; &NewLine; Node temp &equals; Node&lowbar;ptr&period;next&semi; &NewLine; Node&lowbar;ptr&period;data &equals; temp&period;data&semi; &NewLine; Node&lowbar;ptr&period;next &equals; temp&period;next&semi; &NewLine; temp &equals; null&semi; &NewLine; &rcub; &NewLine; public static void printList&lpar;Node head&rpar; &lbrace; &NewLine; while &lpar;head &excl;&equals; null&rpar; &lbrace; &NewLine; System&period;out&period;print&lpar;"->&semi;"&plus;head&period;getData&lpar;&rpar;&rpar;&semi; &NewLine;&Tab;&Tab;head &equals; head&period;getNext&lpar;&rpar;&semi; &NewLine;&Tab; &rcub; &NewLine;&Tab;System&period;out&period;println&lpar;&rpar;&semi; &NewLine;&Tab;&rcub; &NewLine;&rcub; &NewLine; &NewLine;<&sol;pre>&NewLine;<p>Run above program&comma; you will get the following output&colon;<&sol;p>&NewLine;<pre>Before deleting &NewLine;->1->2->3->4->5->6->7->8->9 &NewLine; &NewLine;After Deleting &NewLine;->1->2->4->5->6->7->8->9 &NewLine;<&sol;pre>&NewLine;<p>As you can see the above output&comma; we have deleted a node with data 3&period;<&sol;p>&NewLine;<p>Hope&comma; you have understood this solution for the above to delete given node from linked list&period; Please share other solutions if you have&period; &colon;&rpar;&period;<&sol;p>&NewLine;<p>Happy learning with us&excl;&excl;&excl;&period;<&sol;p>&NewLine;<div align&equals;"center"><iframe style&equals;"width&colon; 120px&semi; height&colon; 240px&semi;" src&equals;"&sol;&sol;ws-in&period;amazon-adsystem&period;com&sol;widgets&sol;q&quest;ServiceVersion&equals;20070822&amp&semi;OneJS&equals;1&amp&semi;Operation&equals;GetAdHtml&amp&semi;MarketPlace&equals;IN&amp&semi;source&equals;ac&amp&semi;ref&equals;qf&lowbar;sp&lowbar;asin&lowbar;til&amp&semi;ad&lowbar;type&equals;product&lowbar;link&amp&semi;tracking&lowbar;id&equals;dineshonjav06-21&amp&semi;marketplace&equals;amazon&amp&semi;region&equals;IN&amp&semi;placement&equals;1788299450&amp&semi;asins&equals;1788299450&amp&semi;linkId&equals;05b0146b0a85f4472697901e353dc276&amp&semi;show&lowbar;border&equals;true&amp&semi;link&lowbar;opens&lowbar;in&lowbar;new&lowbar;window&equals;true&amp&semi;price&lowbar;color&equals;333333&amp&semi;title&lowbar;color&equals;0066c0&amp&semi;bg&lowbar;color&equals;ffffff" frameborder&equals;"0" marginwidth&equals;"0" marginheight&equals;"0" scrolling&equals;"no"><br &sol;>&NewLine;<&sol;iframe><&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;reverse-linked-list&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;print-nodes-at-k-distance-from-the-root&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '4251'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago