Build a singly linked list · Compiler

Build a singly linked list

Build a singly linked list in the given order and apply the insertions in sequence. HEAD x makes x the new head. AFTER i x inserts x immediately after the node currently in position i, counting the head as position 1.

Input
Read n q (0 <= n <= 50, 1 <= q <= 50), then n initial integer values in head-to-tail order. Next read q commands, one per line: HEAD x or AFTER i x. Every i is a valid position at the time of insertion. All values are in [-1000, 1000].
Output
Print the final values from head to tail on one line, separated by spaces.

Compiler Explorer runs your code when you run or check it.

Loading your exercise draft…