Remove duplicates from a linked List in C#

Problem source: HackerRank -More Linked Lists

You have a linked list that has n Nodes and their data is in ascending order. A Node object has an integer data field, data, and a Node instance pointer, , pointing to another node (i.e.: the next node in a list). our task is to write a method to remove duplicates from this list and print remaining nodes.

Sample Input

6
1
2
2
3
3
4

Sample Output

1 2 3 4 

Explanation

N is total number of Nodes in linked list. The data, 2 and 3 occurs more than one so we need to delete duplicate nodes from the linked list. Once all the duplicate nodes are deleted then print this linked list , which is 1, 2, 3, 4.

class MoreLinkedLists
    {
        public class Node
        {
            public int data;
            public Node next;
            public Node(int d)
            {
                data = d;
                next = null;
            }
        }
        static void Main(string[] args)
        {
            Node head = null;
            int T = Int32.Parse(Console.ReadLine());
            while (T-- > 0)
            {
                int data = Int32.Parse(Console.ReadLine());
                head = Insert(head, data);
            }

            head = removeDuplicates(head);
            display(head);
            Console.ReadKey();
        }

        public static Node removeDuplicates(Node head)
        {
            if (head != null)
            {
                Node start = head;
                while (start != null)
                {
                    if (start.next != null && start.data == start.next.data)
                    {
                        start.next = start.next.next;
                    }
                    else
                    {
                        start = start.next;
                    }
                }
            }
            return head;
        }

        public static Node Insert(Node head, int data)
        {
            Node p = new Node(data);
            if (head == null)
            {
                head = p;
            }
            else if (head.next == null)
            {
                head.next = p;
            }
            else
            {
                Node start = head;
                while (start.next != null)
                {
                    start = start.next;
                }
                start.next = p;
            }
            return head;
        }

        public static void display(Node head)
        {
            Node start = head;
            while (start != null)
            {
                Console.Write(start.data + " ");
                start = start.next;
            }
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s