Infinite Circular Doubly Linked List

2.6

7 votes
Data Structures, Linked list, Doubly and Circular Linked List, Easy
Problem

You are given a circular doubly linked list that contains integers as the data in each node. These data on each node is distinct. You have developed a special algorithm that prints the three continuous elements of the list, starting from the first element or head of the list and runs for infinite time. For example, if the list is {1,9,12,7}, then the output of the algorithm will be {1,9,12,9,12,7,12,7,1,...}. The output contains the infinite number of elements because it is a circular list. 

You are given only a part of the output that has been returned by the algorithm. Your task is to determine the number of elements available in the original list and print the respective elements.

Note

  • It is guaranteed that the provided part of the output of the algorithm is sufficient enough to calculate the size of the original list. 
  • Please read the sample explanation carefully and use the following definition of the doubly linked list:
class Node {
    Object data;
    Node next;
    Node prev;
}

Input format

  • First line: Integer N that denotes the length of the list which is returned as the output of the algorithm
  • Next line: N space-separated integers that denote the elements of the list which is returned as the output of the algorithm

Output format

Your task is to print the output in the following format:

  • First line: Length of the original list
  • Second line: Space-separated integers that denote the elements of the original list

Constraints

1N1051A[i]109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The hidden list is actually [7,12,8,13] with 4 elements hence the output is 4.

Note: You can see that the above list actually matches the output of the program of Alice 7,12,8,12,8,13,8,13,7,13,7,12...

Editor Image

?