import java.util.*;
class LinkedList {
private class Node{
int data;
Node next;
}
private Node head;
private Node tail;
private int size;
public void addLast(int data){
Node nn = new Node();
nn.data = data;
nn.next = null;
if(size>0){
this.tail.next = nn;
}
if(size == 0){
this.head = nn;
this.tail = nn;
this.size=1;
}
else {
this.tail = nn;
this.size++;
}
}
public int getFirst() throws Exception{
if(size == 0 ){
throw new Exception(“List is Empty”);
}
else
return this.head.data;
}
int detectAndRemoveLoop(Node node)
{
Node slow = node, fast = node;
while (slow != null && fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
// If slow and fast meet at same point then loop is present
if (slow == fast) {
removeLoop(slow, node);
return 1;
}
}
return 0;
}
// Function to remove loop
void removeLoop(Node loop, Node curr)
{
Node ptr1 = null, ptr2 = null;
/* Set a pointer to the beging of the Linked List and
move it one by one to find the first node which is
part of the Linked List */
ptr1 = curr;
while (1 == 1) {
/* Now start a pointer from loop_node and check if it ever
reaches ptr2 */
ptr2 = loop;
while (ptr2.next != loop && ptr2.next != ptr1) {
ptr2 = ptr2.next;
}
/* If ptr2 reahced ptr1 then there is a loop. So break the
loop */
if (ptr2.next == ptr1) {
break;
}
/* If ptr2 did't reach ptr1 then try the next node after ptr1 */
ptr1 = ptr1.next;
}
/* After the end of loop ptr2 is the last node of the loop. So
make next of ptr2 as NULL */
ptr2.next = null;
}
// Function to print the linked list
void printList(Node node)
{
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
}
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
LinkedList list = new LinkedList();
while(sc.nextInt()!=-1){
list.addLast(sc.nextInt());
}
list.head = getFirst();
head.next.next.next.next.next = head.next.next;
list.printList(head);
detectAndRemoveLoop(head);
}
}