Reverse a linked list · Compiler

Reverse a linked list

Reverse the links among the first k nodes of a singly linked list. Attach the reversed prefix to the remaining nodes, keeping the suffix in its original order. For k = 0, keep the entire list in its original order.

Input
Read n k (1 <= n <= 100, 0 <= k <= n), followed by n integer values in head-to-tail order. Values are in [-1000, 1000].
Output
Print the values from the resulting head to the tail on one line, separated by spaces.

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

Loading your exercise draft…