Linked List Cycle#7454

Linked List Cycle

1 saniye
128 megabayt
Çok kolay

Consider the sequence of numbers x[0], x[1], ..., x[n-1], generated with the next formula:

x[0] = ,

x[i] = ( * x[i-1] * x[i-1] + * x[i-1] + ) mod , where < <

Create a Linked List from these numbers: List = x[0] → x[1] → ... → x[n-1].

Let = x[n] mod . If x[n] < / , let tail → next of your list point to the -th element. Otherwise tail → next = NULL. Setup the value of tail → next according to this condition.

Consider the example: = , = , = , = , = , = . Here List = → → → → , x[n] = , so = < / = / and tail → next points to the third element that equals (elements' positions in a List starts from ).

prb7454.gif

Write a method hasCycle that determines if a List has a cycle in it.

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
    int hasCycle(void); // Returns 1 if a List has a cycle, and 0 otherwise
};

You can create (use) additional methods if needed.

Giriş

One line contains six numbers: , , , , , . All numbers are integers in the range from to , and are greater than .

Çıktı

Print if the Linked List has a cycle and otherwise.

Örnekler

Örnek 1

Loading
Loading