Rotate List#7453

Rotate List

1 saniye
64 megabayt
Çok kolay

Given an array of integers. Create a linked list from these numbers.

Implement a method RotateRight that rotates the linked list to the right by places, where is a 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 code according to the following interfaces:

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 the linked list
};
class Node:
    def __init__(self, data: int):
        self.data = data
        self.next = None

class List:
    def __init__(self):
        self.head = None
        self.tail = None
    def AddToTail(self, val: int) -> None:
        """Add number val to the end of the linked list"""
        pass
    def RotateRight(self, k: int) -> None:
        """Rotate the list to the right by k places"""
        pass
    def Print(self) -> None:
        """Print the elements of the linked list"""
        pass

You may create and use additional methods if needed.

Giriş

The first line contains the number (1 \le n \le 100). The second line contains integers. Each of the following lines contains one integer (0 \le k \le 10^9).

Çıktı

For each value of , print in a separate line the elements of the list after it is rotated to the right by places. Print the elements of the linked list using the Print method.

Örnekler

Örnek 1

Loading
Loading