FLOYD'S CYCLE LINKED LIST

how do we make the linked list as circular one? where last element point to any one element except the null.

First form linear linked list, then have access to last node(tail) and start iteration from head(using a pointer say ptr), then you may do this : tail->next = ptr where ptr can be any node!

1 Like

we have to initialise it in buildelinked list function right?

can you help me with code for the same?

do it this way:
node *head = buildList()
node *tail // get the last node address into it!
int cycle_element // stores value of nodewhere cycle begins
node *ptr = head
while(ptr!=NULL){
if(ptr->val==cycle_element)
tail->next = ptr,break;
else
ptr=ptr->next;
}

1 Like

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.