Example #1
0
        public string DLLtoString(DLLnode node)
        {
            StringBuilder sb = new StringBuilder();

            while (node != null)
            {
                sb.Append(node.val);
                node = node.next;
            }
            return(sb.ToString());
        }
Example #2
0
        public string solution(string S)
        {
            // deal with special and corner cases
            if ((S == null || S == ""))
            {
                return("");
            }
            if (S.Length == 1)
            {
                return(S);
            }
            if (S.Length == 2)
            {
                if (S[0] == S[1])
                {
                    return("");
                }
                else
                {
                    return(S);
                }
            }

            // string length is 3 or more


            // Load the string into a Doubly Linked List
            //LinkedList<char> list = new LinkedList<char>(S.ToCharArray());
            DLLnode list = new DLLnode(S[0]);

            list.previous = null;

            DLLnode nCurr = list;
            DLLnode nNext;
            DLLnode nPrev = null;

            for (int i = 1; i < S.Length; i++)
            {
                nNext          = new DLLnode(S[i]);
                nCurr.next     = nNext;
                nNext.previous = nCurr;

                nCurr = nCurr.next;
            }

            string transformed = DLLtoString(list);

            // Process the Doubly Linked List
            nCurr = list;
            while (nCurr != null)
            {
                nNext = nCurr.next;
                nPrev = nCurr.previous;

                if (nNext == null)
                {
                    break;
                }

                if (nCurr.val == nNext.val)
                {
                    // take out those two, careful if first or last elements
                    if (nCurr == list)
                    {
                        if (nCurr.next != null)
                        {
                            nCurr = nCurr.next.next;
                        }
                        list = nCurr;
                    }
                    else
                    {
                        // 2 elements somewhere in the middle of the list, unlink them
                        // unlink the current one
                        nCurr.previous.next = nNext.next;
                        nCurr = nPrev;  // go back 1 node, because the new next might also match

                        if (nNext.next != null)
                        {
                            nNext.next.previous = nCurr;    // since nCurr was alread updated
                        }
                    }
                }
                else
                {
                    // nCurr != nNext, keep both and move nCurr up by one
                    nCurr = nCurr.next;
                }
                transformed = DLLtoString(list);
            }

            transformed = DLLtoString(list);
            return(transformed);
        }