HTML & JS
import java.io.*;


public class FlattenLL {
    public static void main(String[] args) {
        ChildListNode a = new ChildListNode(1);
        ChildListNode b = new ChildListNode(2);
        ChildListNode c = new ChildListNode(3);
        ChildListNode d = new ChildListNode(4);
        ChildListNode e = new ChildListNode(5);
        ChildListNode f = new ChildListNode(6);
        ChildListNode g = new ChildListNode(7);
        ChildListNode h = new ChildListNode(8);
        ChildListNode i = new ChildListNode(9);

        a.next = b;
        b.next = c;
        c.next = d;
        b.child = e;
        d.child = g;
        e.next = f;
        e.child = h;
        g.child = i;
        FlattenLL flattenLL = new FlattenLL();
        flattenLL.traverse(flattenLL.flattenedList(a));

    }

    public void traverse(ChildListNode list) {
        while (list != null) {
            System.out.println(list.val+" ");
            list = list.next;
        }
    }


    public ChildListNode flattenedList(ChildListNode list) {
        ChildListNode res = null;
        ChildListNode parPointer = null;
        ChildListNode childList = null;
        ChildListNode childPointer = null;

        res = list;
        parPointer = list;

        while (parPointer != null) {
            if (parPointer.child != null) {
                if (childList == null) {
                    childList = parPointer.child;
                    childPointer = childList;
                    while (childPointer.next != null) {
                        childPointer = childPointer.next;
                    }
                } else {
                    childPointer.next = parPointer.child;
                    childPointer = childPointer.next;
                }
            }

            if (parPointer.next != null)
                parPointer = parPointer.next;
            else {
                parPointer.next = childList;
                parPointer = parPointer.next;
                childPointer = null;
                childList = null;
            }
        }
        return res;
    }
}

class ChildListNode {
    int val;
    ChildListNode next;
    ChildListNode child;
    public ChildListNode(int val) {
        this.val = val;
    }
}

Generated URL:

Output:

1 
2 
3 
4 
5 
6 
7 
8 
9 
Report Bug