replied. check that…
Linked List Floyd algorithm cycle detection
#include
using namespace std;
class node{
public:
int data;
node *next;
//constructor
node(int d){
data = d;
next = NULL;
}
};
//passing a pointer variable reference(head)
//we pass head by reference beacause we want change in it
void insertAtHead(node*&head , int data){
node *n = new node(data);
n->next=head;
head=n;
}
int length(node *head){
int len =0;
while(head!=NULL){
head=head->next;
len=len+1;
}
return len;
}
void insertAtTail(node*&head, int data){
if(head==NULL){
head = new node(data);
return;
}
node *tail=head;
while(tail->next!=NULL){ //tail should be at last node
tail=tail->next;
}
tail->next=new node(data);
return;
}
void insertInMiddle(node*&head, int data , int p){
if(head==NULL || p==0){
insertAtHead(head,data);
}
else if(p>length(head)){
insertAtTail(head, data);
}
else{
//Insert in middle
//take p-1 jumps
int jump=1;
node*temp=head;
while(jump<=p-1){
temp = temp->next;
jump +=1;
}
node*n = new node(data);
n->next=temp->next;
temp->next=n;
}
}
void print(node*head){
while(head!=NULL){
cout<<head->data<<"-->";
head=head->next;
}
}
void deleteAtHead(node*&head){
if(head==NULL){
return;
}
node *temp=head;
head=head->next;
delete temp;
return;
}
void deleteAtTail(node *&head){
node*prev=NULL;
node*temp=head;
while(temp->next!=NULL){
prev=temp;
temp=temp->next;
}
delete temp;
prev->next=NULL;
return;
}
void deleteInTheMIddle(node*&head,int p){
if(head==NULL){
return;
}
int jump=1;
node*temp=head;
node*prev;
while(jump<=p-1){
prev=temp;
temp=temp->next;
jump +=1;
}
prev->next=temp->next;
delete temp;
}
bool searchRecursive(node *head, int key){
if(head==NULL){
return false;
}
//Rec case
if(head->data==key){
return true;
}
else{
return searchRecursive(head->next,key);
}
}
bool searchIterative(node *head, int key){
while(head!=NULL){
if(head->data==key){
return true;
}
head = head->next;
}
return false;
}
void buidList(node *&head){
int data;
cin>>data;
while(data!=-1){
insertAtTail(head,data);
cin>>data;
}
}
node* midPoint(node*head){
if(head==NULL || head->next==NULL){
return head;
}
node *slow =head;
node* fast = head->next;
while(fast!=NULL && fast->next!=NULL){
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
istream& operator>>(istream &is , node*&head){
buidList(head);
return is;
}
ostream& operator<<(ostream &os , node*head){
print(head);
return os;
}
nodemerge(nodea , node*b){
if(a==NULL){
return b;
}
else if(b==NULL){
return a;
}
node* c;
//compare a and b for smaller node
if(a->data < b->data){
c=a;
c->next = merge(a->next , b);
}
else{
c=b;
c->next = merge(a,b->next);
}
return c;
}
node*floydAlgo(node *head){
if(head==NULL || head->next==NULL){
return head;
}
node*fast=head;
node*slow=head;
//1 step: check for the cycle
while(fast!=NULL && fast->next!=NULL){
fast=fast->next->next;
slow = slow->next;
//if loop detected
if(slow==fast){
cout<<"Cycle is Present"<<slow->next->data<<endl;
slow=head;
node*prev=NULL;
//remove here
// cout << slow->data<<" "<< fast->data;
while(slow!=fast){
prev=fast;
slow =slow->next;
fast=fast->next;
}
prev->next=NULL;
return head;
}
// dont put else here
}
//simply return
return head;
}
int main() {
node *head = new node(50);
head->next = new node(20);
head->next->next = new node(15);
head->next->next->next = new node(4);
head->next->next->next->next = new node(10);
/* Create a loop for testing */
head->next->next->next->next->next = head->next->next;
// head = mergeSort(head);
head = floydAlgo(head);
cout<<head<<endl;;
return 0;
}
ye code aapne he bheja hai karke dekho isme
here u corrected my code and sent me the link and i am doing what you are saying but still getting the wrong output
just add this line in ur code
u will get 15