Example #1
0
        public static ListChildNode FlattenALinkedList(ListChildNode node)
        {
            if (node == null)
            {
                return(null);
            }

            ListChildNode head = node;
            ListChildNode tail = node;

            while (tail.next != null)
            {
                tail = tail.next;
            }

            while (node != tail)
            {
                if (node.child != null)
                {
                    tail.next = node.child;

                    ListChildNode temp = node.child;
                    while (temp.next != null)
                    {
                        temp = temp.next;
                    }
                    tail = temp;
                }

                node = node.next;
            }

            return(head);
        }
Example #2
0
        public static void Init(string[] args)
        {
            ListChildNode root = new ListChildNode(1);

            root.next                       = new ListChildNode(2);
            root.next.child                 = new ListChildNode(5);
            root.next.child.next            = new ListChildNode(6);
            root.next.child.child           = new ListChildNode(8);
            root.next.next                  = new ListChildNode(3);
            root.next.next.next             = new ListChildNode(4);
            root.next.next.next.child       = new ListChildNode(7);
            root.next.next.next.child.child = new ListChildNode(9);

            Console.WriteLine(PrintListChildNode(FlattenALinkedList(root)));
        }
Example #3
0
        private static string PrintListChildNode(ListChildNode node)
        {
            string result = string.Empty;

            while (node != null)
            {
                if (string.IsNullOrEmpty(result))
                {
                    result = Convert.ToString(node.val);
                }
                else
                {
                    result = result + "," + node.val;
                }

                node = node.next;
            }

            return(result);
        }