views:

775

answers:

3

“Find a recurring element in a linked list of unicode characters. If a unicode character/s is found to have a duplicate then just delete that repeating node and adjust the list. The constraint was to not to use any extra memory.”

My answer:
Assuming that the unicode char doesn't include surrogate pair char As I am using c#

I don't know how can you find a duplicate character unless you know the values of the previous nodes while traversing the list, and to maintain the previous values you will need extra memory (hash table).

Guys, can you think of any solution to this question? It's an interview question on one of the sites. Also is it possible to solve this in O(n) time?
Here is my Implementation. Can You give feedback on it so that I can make it better?

public static void RemoveDup(Node head)
        {
            Node current1 = head;
            Node current2;



            while (current1 != null)
            {
                current2 = current1;
                while (current2 != null)
                {
                    if (current2.Next!=null && current1.Data == current2.Next.Data)
                    {
                        Node temp = current2.Next.Next;
                        current2.Next = temp;
                        current2=current1;
                        continue;                        
                    }

                    current2 = current2.Next;
                    if (current2 == null)
                        break;

                }

                current1 = current1.Next;
                if (current1 == null)
                    break;
            }


        }
+3  A: 

For each element in the list, search the list for that element and delete it starting at that element's position until you're out of elements.

I'll leave implementation and other choices to you.

Brian
Yaa ...may be I was thinking of solution which runs in O(n) time and hence never thought of this.Any idea on how can we get this done in O(n) time
Learner
I can't think of anything that would do this O(n) AND not use any additional memory
Brian
A: 

The only way I can see to do this without saving previously-seen values somehow would be to use nested loops. Outer loop is "traverse the whole list", inner loop is "traverse the whole list and delete copies of the item pointed at by the outer loop".

tex
Nitpick: The inner loop can traverse the rest of the list.
Henk Holterman
A: 

Sort the list - then all duplicates will be in a row. That will take O(nlogn) time. Of course that assumes that you can sort the list (maybe the order is important?) and that you can sort it inplace (no extra memory).

Niki Yoshiuchi