#include
using namespace std;
class node{
public:
int data;
node * next;
node( int d){
data = d;
next = NULL;
}
};
void insertattail(node*&head,int data)
{
if(head==NULL)
{
head=new node(data);
return;
}
nodetail=head;
while(tail->next!=NULL)
{
tail=tail->next;
}
tail->next=new node(data);
return;
}
void print(nodehead)
{
while(head!=NULL)
{
cout<data;
head=head->next;
}
}
void buildlist(node*&head)
{
int data;
cin>>data;
while(data!=-1)
{
insertattail(head,data);
cin>>data;
}
}
node* detectcycle(node * head)
{
node* sp = head;
node* fp = head;
while(sp!=NULL)
{
sp= sp->next;
fp = fp->next->next;
if(sp==fp)
{
break;
}
}
return fp;
}
void breakcycle(node * head, node* looppt)
{
if(looppt==head)
{
node* kk = head->next;
while(kk->next!=head){
kk= kk->next;
}
kk->next=NULL;
return;
}
node * prev = looppt;
node* sp = head;
while(sp!=looppt){
sp= sp->next;
prev = looppt;
looppt= looppt->next;
}
prev->next = NULL;
}
int main(){
node * head = NULL;
buildlist(head);
breakcycle(head,detectcycle(head));
print(head);
}