Rotate List
Given an array of integers. Create a Linked List from these numbers.
Write a method RotateRight
that rotates the Linked List to the right by k places, where k is non-negative integer.
For example, if List = 1 → 2 → 3 → 4 → 5 → NULL and k = 2, after rotation List = 4 → 5 → 1 → 2 → 3 → NULL.
Write the code according to the next interface:
class Node
{
public:
int data;
Node *next;
Node() : next(NULL) {};
Node(int data, Node *next = NULL) : data(data), next(next) {};
};
class List
{
public:
Node *head, *tail;
List() : head(NULL), tail(NULL) {};
void AddToTail(int val); // Add number val to the end of the Linked List
void RotateRight(int k) // Rotate the list to the right by k places
void Print(void); // Print the elements of Linked List
};
You can create (use) additional methods if needed.
Input
The first line contains number n (1 ≤ n ≤ 100). The second line contains n integers. Each of the next lines contains one number k (0 ≤ k ≤ 10^9
).
Output
For each number k print in the separate line the elements of the List after rotation to the right by k places. Print the elements of Linked List using Print method.