A natural number N is read, followed by N integer values read one at a time. Build a singly linked list with dynamic allocation such that after each insertion the list remains sorted in ascending order. For equal values, the relative order does not matter.
At the end, print the values in the list by traversing it from head to tail, separated by single spaces, followed by a newline.
Use a structure Node { int val; Node* next; } with new for allocation. Do not use STL containers.