how do I take input such that it is stored as LL with cycle?
and also check if my logic to remove cycle correct.
How to take input in the question?
** Taking Input **
as it is mentioned in the question that ,the value start repeating once the cycle gets completed. so we detect the point from where the value starts repeating and stop inserting values to our linked List , so we can make a map or array to store the count of elements in the input and the moment we encounter element which has previosly encountered we will stop inserting values to out array
void buildlist(node*&head)
{
int data;
cin>>data;
int a[100000]={0}; // a[i] store the count of i
while(data!=-1){
if(a[data]==0){
a[data]++;
insertattail(head,data);
cin>>data;
}
else break;
}
while(data!=-1){cin>>data;}
}
in your code :
void RemoveCycle(node* &head){
if(head==NULL){
return;
}
node *slow=head;
node *fast=head;
while(fast!=slow){ // add the condition fast!=NULL and fast->next!=NULL
fast=fast->next->next;
slow=slow->next;
} // add if condition slow==fast as you need to implement below algo only when
// cycle is detected
slow=head;
while(fast!=slow){
slow=slow->next;
fast=fast->next;
}
// while(fast->next!=slow){
// fast=fast->next; there is no need for these lines
// }
fast->next=NULL;
return;
}
u can refer this code for hint :- https://ide.codingblocks.com/s/241954
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.