# Remove duplicates from an unsorted linked list

In previous post we are learning about how to Delete duplicate nodes in sorted linked list, In this we are view examples how to remove duplicates nodes in unarranged (unsorted) linked list.

Approaches: In this case Linked list is not sorted form . So we are need to compare every element to other element of linked list. When find two similar nodes then removing every second similar nodes.

In this process we are need nested while-loops. First is outer loop those are hold the address of current nodes and that are useful to execute inner while-loop. inner loop are check condition of outer loop current node value. If there are similar to inner loop current node. then we are remove of this node. and node not similar then inner loop iterates linked list nodes. And are checking other nodes.

When completes of inner loop iterations. Then outer loop pointer are visit next upcoming nodes and again repeat this process. When no next upcoming nodes are exist stop the execution. This process time complexity is O(n^2) where n is number of linked list nodes.

Suppose linked list contain following (1, 2, 9, 4, 9, 3, 7, 2, 1) node in a sequence.

Here given code implementation process.

``````// Java Program to
// Delete duplicate nodes in unsorted linked list
class Node
{
public int data;
public Node next;
public Node(int data, Node next)
{
this.data = data;
this.next = next;
}
}
{
// Class constructors
{
}
// Insert new element at beginning position
public void insert(int value)
{
// Create new node
Node node = new Node(value, this.head);
}
// Display all node value
public void display()
{
{
while (temp != null)
{
// Display node value
System.out.print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
else
{
}
}
public void removeNode()
{
{
}
else
{
// Auxiliary variable
Node hold = null;
Node initial = null;
Node current = null;
// Outer loop
while (temp != null)
{
current = temp;
initial = current.next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial != null)
{
if (temp.data == initial.data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial.next;
if (hold != null)
{
current.next = initial;
// remove node
hold = null;
}
}
// Visit to next node
temp = temp.next;
}
}
}
public static void main(String[] args)
{
System.out.println("\nBefore Delete ");
System.out.println("\nAfter Delete ");
}
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````// Include header file
#include <iostream>
using namespace std;
// C++ Program to
// Delete duplicate nodes in unsorted linked list
class Node
{
public:
int data;
Node *next;
Node(int data, Node *next)
{
this->data = data;
this->next = next;
}
};
{
public:
// Class constructors
{
}
// Insert new element at beginning position
void insert(int value)
{
// Create new node
Node *node = new Node(value, this->head);
}
// Display all node value
void display()
{
{
cout << "Linked List Element :";
while (temp != NULL)
{
// Display node value
cout << "  " << temp->data;
// Visit to next node
temp = temp->next;
}
}
else
{
cout << "Empty Linked list" << endl;
}
}
void removeNode()
{
{
}
else
{
// Auxiliary variable
Node *hold = NULL;
Node *initial = NULL;
Node *current = NULL;
// Outer loop
while (temp != NULL)
{
current = temp;
initial = current->next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial != NULL)
{
if (temp->data == initial->data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial->next;
if (hold != NULL)
{
current->next = initial;
// remove node
delete hold;
hold = NULL;
}
}
// Visit to next node
temp = temp->next;
}
}
}
};
int main()
{
cout << "\nBefore Delete " << endl;
cout << "\nAfter Delete " << endl;
return 0;
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````// Include namespace system
using System;
// Csharp Program to
// Delete duplicate nodes in unsorted linked list
public class Node
{
public int data;
public Node next;
public Node(int data, Node next)
{
this.data = data;
this.next = next;
}
}
{
// Class constructors
{
}
// Insert new element at beginning position
public void insert(int value)
{
// Create new node
Node node = new Node(value, this.head);
}
// Display all node value
public void display()
{
{
while (temp != null)
{
// Display node value
Console.Write("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
else
{
}
}
public void removeNode()
{
{
}
else
{
// Auxiliary variable
Node hold = null;
Node initial = null;
Node current = null;
// Outer loop
while (temp != null)
{
current = temp;
initial = current.next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial != null)
{
if (temp.data == initial.data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial.next;
if (hold != null)
{
current.next = initial;
// remove node
hold = null;
}
}
// Visit to next node
temp = temp.next;
}
}
}
public static void Main(String[] args)
{
Console.WriteLine("\nBefore Delete ");
Console.WriteLine("\nAfter Delete ");
}
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````<?php
// Php Program to
// Delete duplicate nodes in unsorted linked list
class Node
{
public \$data;
public \$next;
public  function __construct(\$data, \$next)
{
\$this->data = \$data;
\$this->next = \$next;
}
}
{
// Class constructors
public  function __construct()
{
}
// Insert new element at beginning position
public  function insert(\$value)
{
// Create new node
}
// Display all node value
public  function display()
{
{
while (\$temp != NULL)
{
// Display node value
echo("  ".strval(\$temp->data));
// Visit to next node
\$temp = \$temp->next;
}
}
else
{
"\n");
}
}
public  function removeNode()
{
{
}
else
{
// Auxiliary variable
\$hold = NULL;
\$initial = NULL;
\$current = NULL;
// Outer loop
while (\$temp != NULL)
{
\$current = \$temp;
\$initial = \$current->next;
// Inner loop
// Remove all node which value is similar to temp node
while (\$initial != NULL)
{
if (\$temp->data == \$initial->data)
{
// Get remove node
\$hold = \$initial;
}
else
{
\$current = \$initial;
}
// Visit to next node
\$initial = \$initial->next;
if (\$hold != NULL)
{
\$current->next = \$initial;
// remove node
\$hold = NULL;
}
}
// Visit to next node
\$temp = \$temp->next;
}
}
}
}

function main()
{
echo("\nBefore Delete \n");
echo("\nAfter Delete \n");
}
main();``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````// Node JS Program to
// Delete duplicate nodes in unsorted linked list
class Node
{
constructor(data, next)
{
this.data = data;
this.next = next;
}
}
{
// Class constructors
constructor()
{
}
// Insert new element at beginning position
insert(value)
{
// Create new node
var node = new Node(value, this.head);
}
// Display all node value
display()
{
{
while (temp != null)
{
// Display node value
process.stdout.write("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
else
{
}
}
removeNode()
{
{
}
else
{
// Auxiliary variable
var hold = null;
var initial = null;
var current = null;
// Outer loop
while (temp != null)
{
current = temp;
initial = current.next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial != null)
{
if (temp.data == initial.data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial.next;
if (hold != null)
{
current.next = initial;
// remove node
hold = null;
}
}
// Visit to next node
temp = temp.next;
}
}
}
}

function main()
{
console.log("\nBefore Delete ");
console.log("\nAfter Delete ");
}
main();``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````#  Python 3 Program to
#  Delete duplicate nodes in unsorted linked list
class Node :
def __init__(self, data, next) :
self.data = data
self.next = next

#  Class constructors
def __init__(self) :

#  Insert new element at beginning position
def insert(self, value) :
#  Create new node

#  Display all node value
def display(self) :
print("Linked List Element :", end = "")
while (temp != None) :
#  Display node value
print("  ", temp.data, end = "")
#  Visit to next node
temp = temp.next

else :

def removeNode(self) :
print("Empty Linked list", end = "")
else :
#  Auxiliary variable
hold = None
initial = None
current = None
#  Outer loop
while (temp != None) :
current = temp
initial = current.next
#  Inner loop
#  Remove all node which value is similar to temp node
while (initial != None) :
if (temp.data == initial.data) :
#  Get remove node
hold = initial
else :
current = initial

#  Visit to next node
initial = initial.next
if (hold != None) :
current.next = initial
#  remove node
hold = None

#  Visit to next node
temp = temp.next

def main() :
print("\nBefore Delete ")
print("\nAfter Delete ")

if __name__ == "__main__": main()``````

#### Output

``````Before Delete
Linked List Element :   1   2   7   1   3   9   4   9   2   1
After Delete
Linked List Element :   1   2   7   3   9   4``````
``````#  Ruby Program to
#  Delete duplicate nodes in unsorted linked list
class Node
# Define the accessor and reader of class Node
attr_accessor :data, :next
def initialize(data, n)
self.data = data
self.next = n
end

end

#  Class constructors
def initialize()
end

#  Insert new element at beginning position
def insert(value)
#  Create new node
end

#  Display all node value
def display()
while (temp != nil)
#  Display node value
print("  ", temp.data)
#  Visit to next node
temp = temp.next
end

else

end

end

def removeNode()
else

#  Auxiliary variable
hold = nil
initial = nil
current = nil
#  Outer loop
while (temp != nil)
current = temp
initial = current.next
#  Inner loop
#  Remove all node which value is similar to temp node
while (initial != nil)
if (temp.data == initial.data)
#  Get remove node
hold = initial
else

current = initial
end

#  Visit to next node
initial = initial.next
if (hold != nil)
current.next = initial
#  remove node
hold = nil
end

end

#  Visit to next node
temp = temp.next
end

end

end

end

def main()
print("\nBefore Delete ", "\n")
print("\nAfter Delete ", "\n")
end

main()``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````// Scala Program to
// Delete duplicate nodes in unsorted linked list
class Node(var data: Int,
var next: Node);

{
// Class constructors
def this()
{
this(null);
}
// Insert new element at beginning position
def insert(value: Int): Unit = {
// Create new node
var node: Node = new Node(value, this.head);
}
// Display all node value
def display(): Unit = {
{
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
else
{
}
}
def removeNode(): Unit = {
{
}
else
{
// Auxiliary variable
var hold: Node = null;
var initial: Node = null;
var current: Node = null;
// Outer loop
while (temp != null)
{
current = temp;
initial = current.next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial != null)
{
if (temp.data == initial.data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial.next;
if (hold != null)
{
current.next = initial;
// remove node
hold = null;
}
}
// Visit to next node
temp = temp.next;
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
println("\nBefore Delete ");
println("\nAfter Delete ");
}
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````// Swift 4 Program to
// Delete duplicate nodes in unsorted linked list
class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int, _ next: Node? )
{
self.data = data;
self.next = next;
}
}
{
// Class constructors
init()
{
}
// Insert new element at beginning position
func insert(_ value: Int)
{
// Create new node
let node: Node = Node(value, self.head);
}
// Display all node value
func display()
{
{
print("Linked List Element :", terminator: "");
while (temp  != nil)
{
// Display node value
print("  ", temp!.data, terminator: "");
// Visit to next node
temp = temp!.next;
}
}
else
{
}
}
func removeNode()
{
{
}
else
{
// Auxiliary variable
var hold: Node? = nil;
var initial: Node? = nil;
var current: Node? = nil;
// Outer loop
while (temp  != nil)
{
current = temp;
initial = current!.next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial  != nil)
{
if (temp!.data == initial!.data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial!.next;
if (hold  != nil)
{
current!.next = initial;
// remove node
hold = nil;
}
}
// Visit to next node
temp = temp!.next;
}
}
}
}
func main()
{
print("\nBefore Delete ");
print("\nAfter Delete ");
}
main();``````

#### Output

``````Before Delete
Linked List Element :   1   2   7   1   3   9   4   9   2   1
After Delete
Linked List Element :   1   2   7   3   9   4``````
``````// Kotlin Program to
// Delete duplicate nodes in unsorted linked list
class Node
{
var data: Int;
var next: Node ? ;
constructor(data: Int, next: Node ? )
{
this.data = data;
this.next = next;
}
}
{
// Class constructors
constructor()
{
}
// Insert new element at beginning position
fun insert(value: Int): Unit
{
// Create new node
val node: Node = Node(value, this.head);
}
// Display all node value
fun display(): Unit
{
{
var temp: Node ? = this.head;
while (temp != null)
{
// Display node value
print("  " + temp.data);
// Visit to next node
temp = temp.next;
}
}
else
{
}
}
fun removeNode(): Unit
{
{
}
else
{
// Auxiliary variable
var temp: Node ? = this.head;
var hold: Node ? = null;
var initial: Node ? ;
var current: Node ? ;
// Outer loop
while (temp != null)
{
current = temp;
initial = current.next;
// Inner loop
// Remove all node which value is similar to temp node
while (initial != null)
{
if (temp.data == initial.data)
{
// Get remove node
hold = initial;
}
else
{
current = initial;
}
// Visit to next node
initial = initial.next;
if (hold != null)
{
current?.next = initial;
// remove node
hold = null;
}
}
// Visit to next node
temp = temp.next;
}
}
}
}
fun main(args: Array < String > ): Unit
{
println("\nBefore Delete ");
println("\nAfter Delete ");
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````
``````package main
import "fmt"
// Go Program to
// Delete duplicate nodes in unsorted linked list
type Node struct {
data int
next * Node
}
func getNode(data int, next * Node) * Node {
return &Node {data,next}
}
}
}
// Insert new element at beginning position
// Create new node
var node * Node = getNode(value, this.head)
}
// Display all node value
var temp * Node = this.head
for (temp != nil) {
// Display node value
fmt.Print("  ", temp.data)
// Visit to next node
temp = temp.next
}
} else {
}
}
} else {
// Auxiliary variable
var temp * Node = this.head
var hold * Node = nil
var initial * Node = nil
var current * Node = nil
// Outer loop
for (temp != nil) {
current = temp
initial = current.next
// Inner loop
// Remove all node which value is similar to temp node
for (initial != nil) {
if temp.data == initial.data {
// Get remove node
hold = initial
} else {
current = initial
}
// Visit to next node
initial = initial.next
if hold != nil {
current.next = initial
// remove node
hold = nil
}
}
// Visit to next node
temp = temp.next
}
}
}
func main() {
fmt.Println("\nBefore Delete ")
fmt.Println("\nAfter Delete ")
}``````

#### Output

``````Before Delete
Linked List Element :  1  2  7  1  3  9  4  9  2  1
After Delete
Linked List Element :  1  2  7  3  9  4``````

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.