Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

How can one find a cycle in the linked list? IF found how
to recognize the cycle and delete that cycle?

Answer Posted / kedar

Ans #6 is the correct version.

//ensure that it not a empty list or list with single node
or list with 2 nodes which is not a cycle.

bool find_cycle(Node* head){

if(head == NULL // empty list
|| head->next == NULL // list with 1 node
|| head->next>next == NULL) // 2 nodes w/o cycle
{
return false;
}

Node* ptr1 = head;
Node* ptr2 = head->next;

while(ptr1 != NULL && ptr2 != NULL && ptr2->next != NULL)
{
if(ptr1 == ptr2){
printf("\nClycle present in thr LinkList\n");
return true;
}
ptr1 = prt1->next;
ptr2 = ptr2->next->next;
}
return false;
}

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is array sort?

834


What is difference between list and linked list?

903


What is meant by balanced binary tree?

830


What is priority queue in data structure?

970


What is the use of data structure in real life?

895


What is ascii sort order?

847


What is data in computer science?

877


Do you know what is linear search?

836


Is selection sort greedy?

832


What is raid (redundant array of inexpensive disks)?

933


Which language is best for learning data structures and algorithms?

899


What is fibonacci search?

953


Can you store different types in an array?

868


Tell me how to find middle element of linked list in one pass?

860


How would you reverse the characters of an array?

873