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.

Before Remove Duplicate Nodes After Remove duplicates nodes

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;
    }
}
public class LinkedList
{
    public Node head;
    // Class constructors
    public LinkedList()
    {
        this.head = null;
    }
    // Insert new element at beginning position
    public void insert(int value)
    {
        // Create new node
        Node node = new Node(value, this.head);
        // Make new head 
        this.head = node;
    }
    // Display all node value
    public void display()
    {
        if (this.head != null)
        {
            System.out.print("Linked List Element :");
            Node temp = this.head;
            while (temp != null)
            {
                // Display node value
                System.out.print("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            System.out.println("Empty Linked list");
        }
    }
    public void removeNode()
    {
        if (this.head == null)
        {
            // When linked list empty
            System.out.print("Empty Linked list");
        }
        else
        {
            // Auxiliary variable
            Node temp = this.head;
            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)
    {
        // Custom linked list
        LinkedList task = new LinkedList();
        task.insert(1);
        task.insert(2);
        task.insert(9);
        task.insert(4);
        task.insert(9);
        task.insert(3);
        task.insert(1);
        task.insert(7);
        task.insert(2);
        task.insert(1);
        System.out.println("\nBefore Delete ");
        task.display();
        task.removeNode();
        System.out.println("\nAfter Delete ");
        task.display();
    }
}

Output

Remove all duplicates from an unsorted linked list in java
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;
    }
};
class LinkedList
{
    public: 
    Node *head;
    // Class constructors
    LinkedList()
    {
        this->head = NULL;
    }
    // Insert new element at beginning position
    void insert(int value)
    {
        // Create new node
        Node *node = new Node(value, this->head);
        // Make new head 
        this->head = node;
    }
    // Display all node value
    void display()
    {
        if (this->head != NULL)
        {
            cout << "Linked List Element :";
            Node *temp = this->head;
            while (temp != NULL)
            {
                // Display node value
                cout << "  " << temp->data;
                // Visit to next node
                temp = temp->next;
            }
        }
        else
        {
            cout << "Empty Linked list" << endl;
        }
    }
    void removeNode()
    {
        if (this->head == NULL)
        {
            // When linked list empty
            cout << "Empty Linked list";
        }
        else
        {
            // Auxiliary variable
            Node *temp = this->head;
            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()
{
    // Custom linked list
    LinkedList *task = new LinkedList();
    task->insert(1);
    task->insert(2);
    task->insert(9);
    task->insert(4);
    task->insert(9);
    task->insert(3);
    task->insert(1);
    task->insert(7);
    task->insert(2);
    task->insert(1);
    cout << "\nBefore Delete " << endl;
    task->display();
    task->removeNode();
    cout << "\nAfter Delete " << endl;
    task->display();
    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;
    }
}
public class LinkedList
{
    public Node head;
    // Class constructors
    public LinkedList()
    {
        this.head = null;
    }
    // Insert new element at beginning position
    public void insert(int value)
    {
        // Create new node
        Node node = new Node(value, this.head);
        // Make new head 
        this.head = node;
    }
    // Display all node value
    public void display()
    {
        if (this.head != null)
        {
            Console.Write("Linked List Element :");
            Node temp = this.head;
            while (temp != null)
            {
                // Display node value
                Console.Write("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            Console.WriteLine("Empty Linked list");
        }
    }
    public void removeNode()
    {
        if (this.head == null)
        {
            // When linked list empty
            Console.Write("Empty Linked list");
        }
        else
        {
            // Auxiliary variable
            Node temp = this.head;
            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)
    {
        // Custom linked list
        LinkedList task = new LinkedList();
        task.insert(1);
        task.insert(2);
        task.insert(9);
        task.insert(4);
        task.insert(9);
        task.insert(3);
        task.insert(1);
        task.insert(7);
        task.insert(2);
        task.insert(1);
        Console.WriteLine("\nBefore Delete ");
        task.display();
        task.removeNode();
        Console.WriteLine("\nAfter Delete ");
        task.display();
    }
}

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 LinkedList
{
    public $head;
    // Class constructors
    public  function __construct()
    {
        $this->head = NULL;
    }
    // Insert new element at beginning position
    public  function insert($value)
    {
        // Create new node
        $node = new Node($value, $this->head);
        // Make new head 
        $this->head = $node;
    }
    // Display all node value
    public  function display()
    {
        if ($this->head != NULL)
        {
            echo("Linked List Element :");
            $temp = $this->head;
            while ($temp != NULL)
            {
                // Display node value
                echo("  ".strval($temp->data));
                // Visit to next node
                $temp = $temp->next;
            }
        }
        else
        {
            echo("Empty Linked list".
                "\n");
        }
    }
    public  function removeNode()
    {
        if ($this->head == NULL)
        {
            // When linked list empty
            echo("Empty Linked list");
        }
        else
        {
            // Auxiliary variable
            $temp = $this->head;
            $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()
{
    // Custom linked list
    $task = new LinkedList();
    $task->insert(1);
    $task->insert(2);
    $task->insert(9);
    $task->insert(4);
    $task->insert(9);
    $task->insert(3);
    $task->insert(1);
    $task->insert(7);
    $task->insert(2);
    $task->insert(1);
    echo("\nBefore Delete \n");
    $task->display();
    $task->removeNode();
    echo("\nAfter Delete \n");
    $task->display();
}
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 LinkedList
{
    // Class constructors
    constructor()
    {
        this.head = null;
    }
    // Insert new element at beginning position
    insert(value)
    {
        // Create new node
        var node = new Node(value, this.head);
        // Make new head 
        this.head = node;
    }
    // Display all node value
    display()
    {
        if (this.head != null)
        {
            process.stdout.write("Linked List Element :");
            var temp = this.head;
            while (temp != null)
            {
                // Display node value
                process.stdout.write("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            console.log("Empty Linked list");
        }
    }
    removeNode()
    {
        if (this.head == null)
        {
            // When linked list empty
            process.stdout.write("Empty Linked list");
        }
        else
        {
            // Auxiliary variable
            var temp = this.head;
            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()
{
    // Custom linked list
    var task = new LinkedList();
    task.insert(1);
    task.insert(2);
    task.insert(9);
    task.insert(4);
    task.insert(9);
    task.insert(3);
    task.insert(1);
    task.insert(7);
    task.insert(2);
    task.insert(1);
    console.log("\nBefore Delete ");
    task.display();
    task.removeNode();
    console.log("\nAfter Delete ");
    task.display();
}
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 LinkedList :
    #  Class constructors
    def __init__(self) :
        self.head = None
    
    #  Insert new element at beginning position
    def insert(self, value) :
        #  Create new node
        node = Node(value, self.head)
        #  Make new head 
        self.head = node
    
    #  Display all node value
    def display(self) :
        if (self.head != None) :
            print("Linked List Element :", end = "")
            temp = self.head
            while (temp != None) :
                #  Display node value
                print("  ", temp.data, end = "")
                #  Visit to next node
                temp = temp.next
            
        else :
            print("Empty Linked list")
        
    
    def removeNode(self) :
        if (self.head == None) :
            #  When linked list empty
            print("Empty Linked list", end = "")
        else :
            #  Auxiliary variable
            temp = self.head
            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() :
    #  Custom linked list
    task = LinkedList()
    task.insert(1)
    task.insert(2)
    task.insert(9)
    task.insert(4)
    task.insert(9)
    task.insert(3)
    task.insert(1)
    task.insert(7)
    task.insert(2)
    task.insert(1)
    print("\nBefore Delete ")
    task.display()
    task.removeNode()
    print("\nAfter Delete ")
    task.display()

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_reader :data, :next
    attr_accessor :data, :next
    def initialize(data, n) 
        self.data = data
        self.next = n
    end

end

class LinkedList 
    # Define the accessor and reader of class LinkedList
    attr_reader :head
    attr_accessor :head
    #  Class constructors
    def initialize() 
        self.head = nil
    end

    #  Insert new element at beginning position
    def insert(value) 
        #  Create new node
        node = Node.new(value, self.head)
        #  Make new head 
        self.head = node
    end

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

        else
 
            print("Empty Linked list", "\n")
        end

    end

    def removeNode() 
        if (self.head == nil) 
            #  When linked list empty
            print("Empty Linked list")
        else
 
            #  Auxiliary variable
            temp = self.head
            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() 
    #  Custom linked list
    task = LinkedList.new()
    task.insert(1)
    task.insert(2)
    task.insert(9)
    task.insert(4)
    task.insert(9)
    task.insert(3)
    task.insert(1)
    task.insert(7)
    task.insert(2)
    task.insert(1)
    print("\nBefore Delete ", "\n")
    task.display()
    task.removeNode()
    print("\nAfter Delete ", "\n")
    task.display()
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 LinkedList(var head: 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);
        // Make new head 
        this.head = node;
    }
    // Display all node value
    def display(): Unit = {
        if (this.head != null)
        {
            print("Linked List Element :");
            var temp: Node = this.head;
            while (temp != null)
            {
                // Display node value
                print("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            println("Empty Linked list");
        }
    }
    def removeNode(): Unit = {
        if (this.head == null)
        {
            // When linked list empty
            print("Empty Linked list");
        }
        else
        {
            // Auxiliary variable
            var temp: Node = this.head;
            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 = {
        // Custom linked list
        var task: LinkedList = new LinkedList();
        task.insert(1);
        task.insert(2);
        task.insert(9);
        task.insert(4);
        task.insert(9);
        task.insert(3);
        task.insert(1);
        task.insert(7);
        task.insert(2);
        task.insert(1);
        println("\nBefore Delete ");
        task.display();
        task.removeNode();
        println("\nAfter Delete ");
        task.display();
    }
}

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 LinkedList
{
    var head: Node? ;
    // Class constructors
    init()
    {
        self.head = nil;
    }
    // Insert new element at beginning position
    func insert(_ value: Int)
    {
        // Create new node
        let node: Node = Node(value, self.head);
        // Make new head 
        self.head = node;
    }
    // Display all node value
    func display()
    {
        if (self.head  != nil)
        {
            print("Linked List Element :", terminator: "");
            var temp: Node? = self.head;
            while (temp  != nil)
            {
                // Display node value
                print("  ", temp!.data, terminator: "");
                // Visit to next node
                temp = temp!.next;
            }
        }
        else
        {
            print("Empty Linked list");
        }
    }
    func removeNode()
    {
        if (self.head == nil)
        {
            // When linked list empty
            print("Empty Linked list", terminator: "");
        }
        else
        {
            // Auxiliary variable
            var temp: Node? = self.head;
            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()
{
    // Custom linked list
    let task: LinkedList = LinkedList();
    task.insert(1);
    task.insert(2);
    task.insert(9);
    task.insert(4);
    task.insert(9);
    task.insert(3);
    task.insert(1);
    task.insert(7);
    task.insert(2);
    task.insert(1);
    print("\nBefore Delete ");
    task.display();
    task.removeNode();
    print("\nAfter Delete ");
    task.display();
}
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 LinkedList
{
    var head: Node ? ;
    // Class constructors
    constructor()
    {
        this.head = null;
    }
    // Insert new element at beginning position
    fun insert(value: Int): Unit
    {
        // Create new node
        val node: Node = Node(value, this.head);
        // Make new head 
        this.head = node;
    }
    // Display all node value
    fun display(): Unit
    {
        if (this.head != null)
        {
            print("Linked List Element :");
            var temp: Node ? = this.head;
            while (temp != null)
            {
                // Display node value
                print("  " + temp.data);
                // Visit to next node
                temp = temp.next;
            }
        }
        else
        {
            println("Empty Linked list");
        }
    }
    fun removeNode(): Unit
    {
        if (this.head == null)
        {
            // When linked list empty
            print("Empty Linked list");
        }
        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
{
    // Custom linked list
    val task: LinkedList = LinkedList();
    task.insert(1);
    task.insert(2);
    task.insert(9);
    task.insert(4);
    task.insert(9);
    task.insert(3);
    task.insert(1);
    task.insert(7);
    task.insert(2);
    task.insert(1);
    println("\nBefore Delete ");
    task.display();
    task.removeNode();
    println("\nAfter Delete ");
    task.display();
}

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}
}
type LinkedList struct {
    head * Node
}
func getLinkedList() * LinkedList {
    return &LinkedList {nil}
}
// Insert new element at beginning position
func(this *LinkedList) insert(value int) {
    // Create new node
    var node * Node = getNode(value, this.head)
    // Make new head 
    this.head = node
}
// Display all node value
func(this LinkedList) display() {
    if this.head != nil {
        fmt.Print("Linked List Element :")
        var temp * Node = this.head
        for (temp != nil) {
            // Display node value
            fmt.Print("  ", temp.data)
            // Visit to next node
            temp = temp.next
        }
    } else {
        fmt.Println("Empty Linked list")
    }
}
func(this LinkedList) removeNode() {
    if this.head == nil {
        // When linked list empty
        fmt.Print("Empty Linked list")
    } 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() {
    // Custom linked list
    var task * LinkedList = getLinkedList()
    task.insert(1)
    task.insert(2)
    task.insert(9)
    task.insert(4)
    task.insert(9)
    task.insert(3)
    task.insert(1)
    task.insert(7)
    task.insert(2)
    task.insert(1)
    fmt.Println("\nBefore Delete ")
    task.display()
    task.removeNode()
    fmt.Println("\nAfter Delete ")
    task.display()
}

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.

New Comment







© 2021, kalkicode.com, All rights reserved