<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>linked list program in java - Wikitechy</title>
	<atom:link href="https://www.wikitechy.com/interview-questions/tag/linked-list-program-in-java/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.wikitechy.com/interview-questions/tag/linked-list-program-in-java/</link>
	<description>Interview Questions</description>
	<lastBuildDate>Mon, 13 Sep 2021 10:24:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.wikitechy.com/interview-questions/wp-content/uploads/2025/10/cropped-wikitechy-icon-32x32.png</url>
	<title>linked list program in java - Wikitechy</title>
	<link>https://www.wikitechy.com/interview-questions/tag/linked-list-program-in-java/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Java Program to reverse a Linked List ?</title>
		<link>https://www.wikitechy.com/interview-questions/java/java-program-to-reverse-a-linked-list/</link>
					<comments>https://www.wikitechy.com/interview-questions/java/java-program-to-reverse-a-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Wed, 14 Jul 2021 00:03:25 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Accenture interview questions and answers]]></category>
		<category><![CDATA[Applied Materials interview questions and answers]]></category>
		<category><![CDATA[Atos interview questions and answers]]></category>
		<category><![CDATA[Capgemini interview questions and answers]]></category>
		<category><![CDATA[CASTING NETWORKS INDIA PVT LIMITED interview questions and answers]]></category>
		<category><![CDATA[CGI Group Inc interview questions and answers]]></category>
		<category><![CDATA[Chetu interview questions and answers]]></category>
		<category><![CDATA[Ciena Corporation interview questions and answers]]></category>
		<category><![CDATA[circular linked list java]]></category>
		<category><![CDATA[Collabera Technologies interview questions and answers]]></category>
		<category><![CDATA[compro technologies interview questions and answers]]></category>
		<category><![CDATA[create linked list java]]></category>
		<category><![CDATA[Dell International Services India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[doubly linked list java]]></category>
		<category><![CDATA[FIS Global Business Solutions India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Flipkart interview questions and answers]]></category>
		<category><![CDATA[how to implement linked list in java]]></category>
		<category><![CDATA[IBM interview questions and answers]]></category>
		<category><![CDATA[Indecomm Global Services interview questions and answers]]></category>
		<category><![CDATA[Infosys Technologies interview questions and answers]]></category>
		<category><![CDATA[javascript linked list]]></category>
		<category><![CDATA[L&T Infotech interview questions and answers]]></category>
		<category><![CDATA[linked list class in java]]></category>
		<category><![CDATA[linked list data structure in java]]></category>
		<category><![CDATA[linked list implementation in java]]></category>
		<category><![CDATA[linked list implementation in java source code]]></category>
		<category><![CDATA[linked list java code]]></category>
		<category><![CDATA[linked list java tutorial]]></category>
		<category><![CDATA[linked list program in java]]></category>
		<category><![CDATA[Mphasis interview questions and answers]]></category>
		<category><![CDATA[NetApp interview questions and answers]]></category>
		<category><![CDATA[Oracle Corporation interview questions and answers]]></category>
		<category><![CDATA[PeopleStrong interview questions and answers]]></category>
		<category><![CDATA[Persistent Systems interview questions and answers]]></category>
		<category><![CDATA[RBS India De interview questions and answers]]></category>
		<category><![CDATA[Reliance Industries Ltd interview questions and answers]]></category>
		<category><![CDATA[reverse linked list java]]></category>
		<category><![CDATA[SAP Labs India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[singly linked list java]]></category>
		<category><![CDATA[sort linked list java]]></category>
		<category><![CDATA[Tech Mahindra interview questions and answers]]></category>
		<category><![CDATA[UnitedHealth Group interview questions and answers]]></category>
		<category><![CDATA[Virtusa Consulting Services Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Wells Fargo interview questions and answers]]></category>
		<category><![CDATA[Wipro Infotech interview questions and answers]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=666</guid>

					<description><![CDATA[Answer : A Linked list contains two things data and the address of the node each node....]]></description>
										<content:encoded><![CDATA[<div class="TextHeading">
<div class="hddn">
<h2 id="java-program-to-reverse-a-linked-list" class="color-pink" style="text-align: justify;">Java Program to reverse a Linked List ?</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>A <a href="https://www.wikitechy.com/technology/quicksort-doubly-linked-list-2/" target="_blank" rel="noopener">Linked list</a> contains two things data and the address of the node each node is linked to the next node.</li>
<li>There can be two solution to reverse a linked list in java.
<ul>
<li><a href="https://www.wikitechy.com/technology/iterative-quick-sort/" target="_blank" rel="noopener">Iterative</a></li>
<li><a href="https://www.wikitechy.com/technology/c-algorithm-write-recursive-function-print-reverse-linked-list/" target="_blank" rel="noopener">Recursive</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="iterative" class="color-purple">Iterative</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Here we have three nodes i.e previousNode, currentNode and nextNode</li>
<li>When currentNode is starting node, then previousNode will be null</li>
<li>Assign currentNode.next to previousNode to reverse the link.</li>
<li>In each iteration move currentNode and previousNode by 1 node.</li>
</ul>
</div>
</div>
<div class="text-center row" style="text-align: justify;">
<div class="col-sm-12">
<div id="bsa-zone_1590522538159-8_123456"></div>
</div>
</div>
<div class="ImageContent">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/interview-questions/java/iterative-method-linked-list.gif" alt=" data- linked-list" /></div>
<div>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-java code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-java code-embed-code">public static Node reverseLinkedList(Node currentNode)<br/>	{<br/>		// For first node, previousNode will be null<br/>		Node previousNode=null;<br/>		Node nextNode;<br/>		while(currentNode!=null)<br/>		{<br/>			nextNode=currentNode.next;<br/>			// reversing the link<br/>			currentNode.next=previousNode;<br/>			// moving currentNode and previousNode by 1 node<br/>			previousNode=currentNode;<br/>			currentNode=nextNode;<br/>		}<br/>		return previousNode;<br/>	}</code></pre> </div>
<h2 id="sample-code-in-java" class="color-pink" style="text-align: justify;">Sample Code in Java:</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-java code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-java code-embed-code">public class LinkedList{<br/> <br/>	private Node head;<br/> <br/>	private static class Node {<br/>		private int value;<br/>		private Node next;<br/> <br/>		Node(int value) {<br/>			this.value = value;<br/> <br/>		}<br/>	}<br/> <br/>	public void addToTheLast(Node node) {<br/> <br/>		if (head == null) {<br/>			head = node;<br/>		} else {<br/>			Node temp = head;<br/>			while (temp.next != null)<br/>				temp = temp.next;<br/> <br/>			temp.next = node;<br/>		}<br/>	}<br/> <br/> <br/>	public void printList(Node head) {<br/>		Node temp = head;<br/>		while (temp != null) {<br/>			System.out.format(&quot;%d &quot;, temp.value);<br/>			temp = temp.next;<br/>		}<br/>		System.out.println();<br/>	}<br/> <br/>	// Reverse linkedlist using this function <br/>	public static Node reverseLinkedList(Node currentNode)<br/>	{<br/>		// For first node, previousNode will be null<br/>		Node previousNode=null;<br/>		Node nextNode;<br/>		while(currentNode!=null)<br/>		{<br/>			nextNode=currentNode.next;<br/>			// reversing the link<br/>			currentNode.next=previousNode;<br/>			// moving currentNode and previousNode by 1 node<br/>			previousNode=currentNode;<br/>			currentNode=nextNode;<br/>		}<br/>		return previousNode;<br/>	}<br/> <br/>	public static void main(String[] args) {<br/>		LinkedList list = new LinkedList();<br/>		// Creating a linked list<br/>		Node head=new Node(5);<br/>		list.addToTheLast(head);<br/>		list.addToTheLast(new Node(6));<br/>		list.addToTheLast(new Node(7));<br/>		list.addToTheLast(new Node(1));<br/>		list.addToTheLast(new Node(2));<br/> <br/>		list.printList(head);<br/>		//Reversing LinkedList<br/>		Node reverseHead=reverseLinkedList(head);<br/>		System.out.println(&quot;After reversing&quot;);<br/>		list.printList(reverseHead);<br/> <br/>	}<br/> <br/>}</code></pre> </div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="output" class="color-pink">Output :</h2>
</div>
</div>
<div class="Output">
<div class="hddn">
<figure class="highlight">
<pre><code class="hljs" data-lang=""><span class="nt">5 6 7 1 2 
After reversing
2 1 7 6 5</span></code></pre>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="recursive-method" class="color-purple">Recursive Method:</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Divide the list in two parts &#8211; first node and rest of the linked list.</li>
<li>Call reverse for the rest of the linked list.</li>
<li>Link rest to first.</li>
<li>Fix head pointer</li>
</ul>
</div>
</div>
<div class="ImageContent">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/interview-questions/java/reverse-linked-list-in-java.jpg" alt=" data- linked-list" /></div>
</div>
</figure>
</div>
</div>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/java/java-program-to-reverse-a-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to make linked list program for the given list ?</title>
		<link>https://www.wikitechy.com/interview-questions/data-structure/how-to-make-linked-list-program-for-the-given-list/</link>
					<comments>https://www.wikitechy.com/interview-questions/data-structure/how-to-make-linked-list-program-for-the-given-list/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 17:24:03 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Accenture interview questions and answers]]></category>
		<category><![CDATA[Altimetrik India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Applied Materials interview questions and answers]]></category>
		<category><![CDATA[Bharti Airtel interview questions and answers]]></category>
		<category><![CDATA[BMC Software interview questions and answers]]></category>
		<category><![CDATA[Capgemini interview questions and answers]]></category>
		<category><![CDATA[CASTING NETWORKS INDIA PVT LIMITED interview questions and answers]]></category>
		<category><![CDATA[CGI Group Inc interview questions and answers]]></category>
		<category><![CDATA[Chetu interview questions and answers]]></category>
		<category><![CDATA[Ciena Corporation interview questions and answers]]></category>
		<category><![CDATA[Collabera Te interview questions and answers]]></category>
		<category><![CDATA[Dell International Services India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[doubly linked list implementation in java]]></category>
		<category><![CDATA[ecare technology labs interview questions and answers]]></category>
		<category><![CDATA[Flipkart interview questions and answers]]></category>
		<category><![CDATA[Genpact interview questions and answers]]></category>
		<category><![CDATA[Globallogic India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[how to create a linked list in java]]></category>
		<category><![CDATA[how to create a node in linked list in java]]></category>
		<category><![CDATA[IBM interview questions and answers]]></category>
		<category><![CDATA[Indecomm Global Services interview questions and answers]]></category>
		<category><![CDATA[insert a node at a specific position in a linked list in java]]></category>
		<category><![CDATA[insert node at beginning of linked list in java]]></category>
		<category><![CDATA[insert node at beginning of linked list java program]]></category>
		<category><![CDATA[insertion in linked list algorithm]]></category>
		<category><![CDATA[java program to insert a node at the end in linked list]]></category>
		<category><![CDATA[linked list add method implementation]]></category>
		<category><![CDATA[linked list data structure in java]]></category>
		<category><![CDATA[linked list implementation in java]]></category>
		<category><![CDATA[linked list in java]]></category>
		<category><![CDATA[linked list in java program]]></category>
		<category><![CDATA[linked list insertion]]></category>
		<category><![CDATA[linked list java]]></category>
		<category><![CDATA[linked list java code]]></category>
		<category><![CDATA[linked list java program]]></category>
		<category><![CDATA[linked list node java]]></category>
		<category><![CDATA[linked list program in java]]></category>
		<category><![CDATA[linked list program in java for insertion and deletion]]></category>
		<category><![CDATA[linked list program in java with explanation]]></category>
		<category><![CDATA[Mphasis interview questions and answers]]></category>
		<category><![CDATA[NetApp interview questions and answers]]></category>
		<category><![CDATA[Oracle Corporation interview questions and answers]]></category>
		<category><![CDATA[SAP Labs India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Sapient Consulting Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[singly linked list algorithm]]></category>
		<category><![CDATA[singly linked list in data structure]]></category>
		<category><![CDATA[singly linked list java]]></category>
		<category><![CDATA[singly linked list program in java]]></category>
		<category><![CDATA[Tech Mahindra interview questions and answers]]></category>
		<category><![CDATA[Tracxn Technologies Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[UnitedHealth Group interview questions and answers]]></category>
		<category><![CDATA[user defined linked list in java]]></category>
		<category><![CDATA[Wipro Infotech interview questions and answers]]></category>
		<category><![CDATA[WM Global Technology Services India Pvt.Ltd Limited (WMGTS) interview questions and answers]]></category>
		<category><![CDATA[Xoriant Solutions Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Yodlee Infotech Pvt Ltd interview questions and answers]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=505</guid>

					<description><![CDATA[Answer : Linked List is a type of Linear Data Structure that is mostly used data structure....
]]></description>
										<content:encoded><![CDATA[<div class="TextHeading">
<div class="hddn">
<h2 id="how-to-make-linked-list-program-for-the-given-list" class="color-pink" style="text-align: justify;">How to make linked list program for the given list ?</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li><a href="https://www.wikitechy.com/interview-questions/data-structure/what-is-linked-list-in-data-structure-with-example/" target="_blank" rel="noopener">Linked List</a> is a type of Linear <a href="https://www.wikitechy.com/interview-questions/data-structure/what-is-data-structure/" target="_blank" rel="noopener">Data Structure</a> that is mostly used data structure after array, which allocates memory dynamically at run time that is it doesn’t require any size initialization as in case of array.</li>
<li>Linked List stores data in the forms of nodes, which is divided into two parts, first part stores the data and second part points to the next node by storing the address of that node.</li>
<li>We have first node which contains Data and Address to next node, and similarly second and third node forming a chain.</li>
</ul>
</div>
</div>
<div class="ImageContent" style="text-align: justify;">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/interview-questions/data-structure/linked-list-in-data-structure.png" alt="Linked List in Data Structure" /></div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="syntax" class="color-blue">Syntax</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>To create code, pattern required is:</li>
</ul>
</div>
</div>
<div class="CodeContent" style="text-align: justify;">
<div class="hddn">
<figure class="highlight">
<pre><code id="code1" class="hljs javascript" data-lang=""><span class="nt">LinkedList&lt;<span class="hljs-built_in">String</span>&gt; linkedList=<span class="hljs-keyword">new</span> LinkedList&lt;<span class="hljs-built_in">String</span>&gt;()</span></code></pre>
</figure>
</div>
</div>
<div class="TextHeading">
<div class="hddn">
<h2 id="sample-code-in-java" class="color-purple" style="text-align: justify;">Sample Code in Java</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-java code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-java code-embed-code">import java.util.Iterator;<br/><br/>import java.util.LinkedList;					<br/><br/>public class LinkedListDemo <br/>{<br/><br/>  public static void main (String[] args) {<br/><br/>  LinkedList&lt;String&gt; linkedList=new LinkedList&lt;String&gt;();<br/><br/>  linkedList.add(&quot;java&quot;);<br/><br/>  linkedList.add(&quot;Is&quot;);<br/><br/>  linkedList.add(&quot;High&quot;);<br/><br/>  linkedList.add(&quot;Level&quot;);<br/><br/>  linkedList.add(&quot;Language&quot;);<br/><br/>  Iterator&lt;String&gt; itr=linkedList.iterator();<br/><br/>  while(itr.hasNext())<br/>  {<br/><br/>   System.out.print(itr.next());<br/><br/>   System.out.print(&quot;\t&quot;);<br/><br/>  }<br/><br/> }<br/><br/>}</code></pre> </div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="output" class="color-purple">Output</h2>
</div>
</div>
<div class="Output">
<div class="hddn">
<figure class="highlight" style="text-align: justify;">
<pre><code class="hljs" data-lang=""><span class="nt">java Is High Level Language</span></code></pre>
</figure>
</div>
</div>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/data-structure/how-to-make-linked-list-program-for-the-given-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to remove duplicates from a sorted linked list ?</title>
		<link>https://www.wikitechy.com/interview-questions/data-structure/how-to-remove-duplicates-from-a-sorted-linked-list/</link>
					<comments>https://www.wikitechy.com/interview-questions/data-structure/how-to-remove-duplicates-from-a-sorted-linked-list/#respond</comments>
		
		<dc:creator><![CDATA[Editor]]></dc:creator>
		<pubDate>Tue, 13 Jul 2021 11:14:06 +0000</pubDate>
				<category><![CDATA[Data Structure]]></category>
		<category><![CDATA[Accenture interview questions and answers]]></category>
		<category><![CDATA[algorithm to remove duplicate elements from linked list]]></category>
		<category><![CDATA[Altimetrik India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Applied Materials interview questions and answers]]></category>
		<category><![CDATA[Bharti Airtel interview questions and answers]]></category>
		<category><![CDATA[BMC Software interview questions and answers]]></category>
		<category><![CDATA[Capgemini interview questions and answers]]></category>
		<category><![CDATA[CASTING NETWORKS INDIA PVT LIMITED interview questions and answers]]></category>
		<category><![CDATA[CGI Group Inc interview questions and answers]]></category>
		<category><![CDATA[Chetu interview questions and answers]]></category>
		<category><![CDATA[Ciena Corporation interview questions and answers]]></category>
		<category><![CDATA[Collabera Te interview questions and answers]]></category>
		<category><![CDATA[Dell International Services India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[find duplicates in linked list java]]></category>
		<category><![CDATA[Flipkart interview questions and answers]]></category>
		<category><![CDATA[geekyants interview questions and answers]]></category>
		<category><![CDATA[Genpact interview questions and answers]]></category>
		<category><![CDATA[Globallogic India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[IBM interview questions and answers]]></category>
		<category><![CDATA[Indecomm Global Services interview questions and answers]]></category>
		<category><![CDATA[linked list algorithm]]></category>
		<category><![CDATA[linked list implementation in java]]></category>
		<category><![CDATA[linked list insertion]]></category>
		<category><![CDATA[linked list interview questions]]></category>
		<category><![CDATA[linked list program in java]]></category>
		<category><![CDATA[merge sort linked list]]></category>
		<category><![CDATA[Mphasis interview questions and answers]]></category>
		<category><![CDATA[NetApp interview questions and answers]]></category>
		<category><![CDATA[Oracle Corporation interview questions and answers]]></category>
		<category><![CDATA[remove duplicates from linked list java]]></category>
		<category><![CDATA[remove duplicates from sorted linked list java]]></category>
		<category><![CDATA[remove duplicates from unsorted linked list]]></category>
		<category><![CDATA[reverse a linked lis]]></category>
		<category><![CDATA[reverse linked list java]]></category>
		<category><![CDATA[SAP Labs India Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Sapient Consulting Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Searches related to remove duplicates from unsorted linked list]]></category>
		<category><![CDATA[singly linked list]]></category>
		<category><![CDATA[sort linked list java]]></category>
		<category><![CDATA[sort linked listmerge two sorted linked lists]]></category>
		<category><![CDATA[Tech Mahindra interview questions and answers]]></category>
		<category><![CDATA[Tracxn Technologies Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[UnitedHealth Group interview questions and answers]]></category>
		<category><![CDATA[Wipro Infotech interview questions and answers]]></category>
		<category><![CDATA[WM Global Technology Services India Pvt.Ltd Limited (WMGTS) interview questions and answers]]></category>
		<category><![CDATA[Xoriant Solutions Pvt Ltd interview questions and answers]]></category>
		<category><![CDATA[Yodlee Infotech Pvt Ltd interview questions and answers]]></category>
		<guid isPermaLink="false">https://www.wikitechy.com/interview-questions/?p=476</guid>

					<description><![CDATA[Answer : To write a removeDuplicates() function which takes a list sorted...]]></description>
										<content:encoded><![CDATA[<div class="TextHeading">
<div class="hddn">
<h2 id="how-to-remove-duplicates-from-a-sorted-linked-list" class="color-pink" style="text-align: justify;">How to remove duplicates from a sorted linked list ?</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>To write a removeDuplicates() function which takes a <a href="https://www.wikitechy.com/interview-questions/latex/what-are-the-list-of-latex/" target="_blank" rel="noopener">list</a> sorted from the list any duplicate nodes to be deleted. The list should be traversed only once.</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="algorithm" class="color-green">Algorithm</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li>Traverse the head (or start) node from the list. Compare each node with its next node while <a href="https://www.wikitechy.com/technology/python-algorithm-circular-linked-list-traversal/" target="_blank" rel="noopener">traversing</a>.</li>
<li>If same as the current node and data of next node to be delete that node. Before we delete a node, we need to store next pointer of the node.</li>
</ul>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="implementation" class="color-green">Implementation</h2>
</div>
</div>
<div class="Content" style="text-align: justify;">
<div class="hddn">
<ul>
<li><a href="https://www.wikitechy.com/tutorials/sql/functions-in-sql" target="_blank" rel="noopener">Functions</a> other than removeDuplicates() are just to create a linked.</li>
<li><a href="https://www.wikitechy.com/tutorials/java/java-linked-list" target="_blank" rel="noopener">Linked list</a> to be tested and removeDuplicates() from the list.</li>
</ul>
</div>
</div>
<div class="ImageContent">
<div class="hddn"><img decoding="async" class="img-responsive center-block aligncenter" src="https://cdn.wikitechy.com/interview-questions/data-structure/how-to-remove-duplicates-from-a-sorted-linked-list.png" alt=" Remove Duplicate From a Sorting Linked List" /></div>
<div>
<h2 id="sample-code-in-java" class="color-blue" style="text-align: justify;">Sample Code in Java</h2>
<div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-javascript code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-javascript code-embed-code">class LinkedListExample <br/>{ <br/>	Node head; // head of list <br/><br/>	/* Linked list Node*/<br/>	class Node <br/>	{ <br/>		int data; <br/>		Node next; <br/>		Node(int d) {data = d; next = null; } <br/>	} <br/><br/>	void removeDuplicates() <br/>	{ <br/>		/*Another reference to head*/<br/>		Node current = head; <br/><br/>		/* Pointer to store the next pointer of a node to be deleted*/<br/>		Node next_next; <br/><br/>		/* do nothing if the list is empty */<br/>		if (head == null)	 <br/>			return; <br/><br/>		/* Traverse list till the last node */<br/>		while (current.next != null) { <br/><br/>			/*Compare current node with the next node */<br/>			if (current.data == current.next.data) { <br/>				next_next = current.next.next; <br/>				current.next = null; <br/>				current.next = next_next; <br/>			} <br/>			else // advance if no deletion <br/>			current = current.next; <br/>		} <br/>	} <br/>					<br/>	/* Utility functions */<br/><br/>	/* Inserts a new Node at front of the list. */<br/>	public void push(int new_data) <br/>	{ <br/>		/* 1 &amp; 2: Allocate the Node &amp; <br/>				Put in the data*/<br/>		Node new_node = new Node(new_data); <br/><br/>		/* 3. Make next of new Node as head */<br/>		new_node.next = head; <br/><br/>		/* 4. Move the head to point to new Node */<br/>		head = new_node; <br/>	} <br/><br/>	/* Function to print linked list */<br/>	void printList() <br/>	{ <br/>		Node temp = head; <br/>		while (temp != null) <br/>		{ <br/>			System.out.print(temp.data+&quot; &quot;); <br/>			temp = temp.next; <br/>		} <br/>		System.out.println(); <br/>	} <br/><br/>	/* Drier program to test above functions */<br/>	public static void main(String args[]) <br/>	{ <br/>		LinkedListExample wikitechylist = new LinkedListExample(); <br/>		wikitechylist.push(63); <br/>		wikitechylist.push(50); <br/>		wikitechylist.push(50); <br/>		wikitechylist.push(42); <br/>		wikitechylist.push(27); <br/>		wikitechylist.push(27); <br/>		<br/>		System.out.println(&quot;LinkedList before removal of duplicates&quot;); <br/>		wikitechylist.printList(); <br/>		<br/>		wikitechylist.removeDuplicates(); <br/>		<br/>		System.out.println(&quot;LinkedList after removal of elements&quot;); <br/>		wikitechylist.printList(); <br/>	} <br/>} </code></pre> </div>
<div class="Output" style="text-align: justify;">
<div class="hddn">
<figure class="highlight"><div class="code-embed-wrapper"> <div class="code-embed-infos"> </div> <pre class="language-markdown code-embed-pre line-numbers"  data-start="1" data-line-offset="0"><code class="language-markdown code-embed-code">LinkedList before removal of duplicates<br/>27 27 42 50 50 63 <br/>LinkedList after removal of elements<br/>27 42 50 63</code></pre> </div></figure>
</div>
</div>
<div class="TextHeading" style="text-align: justify;">
<div class="hddn">
<h2 id="time-complexity" class="color-blue">Time Complexity</h2>
</div>
</div>
<div class="Content">
<div class="hddn">
<ul>
<li style="text-align: justify;">O(n) where n is number of nodes in the given linked list.</li>
</ul>
</div>
</div>
</div>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.wikitechy.com/interview-questions/data-structure/how-to-remove-duplicates-from-a-sorted-linked-list/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
