You are provided list_rotate.c. Implement the function rotate
which, given the head of a linked list, the length of the list, and a non-negative integer input steps
, rotates the list to the right by steps
.
For example:
The output should look exactly like the following:
$ dcc list_rotate.c -o list_rotate
$ ./list_rotate
How many numbers in list?: 5
0 1 2 3 4
What number to rotate by?: 0
[0, 1, 2, 3, 4]
$ ./list_rotate
How many numbers in list?: 5
0 1 2 3 4
What number to rotate by?: 2
[3, 4, 0, 1, 2]
$ ./list_rotate
How many numbers in list?: 5
0 1 2 3 4
What number to rotate by?: 6
[4, 0, 1, 2, 3]
steps >= 0
.steps <= list length
.rotate
. You may make your own functions if wish.When you think your program is working, you can use CSE autotest to test your solution.
$ 1511 csesoc-autotest list_rotate
The starter code for this exercise was largely taken from the Week 08 COMP1511 lab exercise — "Reverse a Linked List".
You can view the solution code to this problem here.