Guest User

Untitled

a guest
Jun 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. /*Please note that it's Function problem i.e.
  2. you need to write your solution in the form of Function(s) only.
  3. Driver Code to call/invoke your function is mentioned above.*/
  4.  
  5. /*The structure of linked list is the following
  6. struct Node
  7. {
  8. int data;
  9. Node* next;
  10. };*/
  11. /*The function removes the loop from the linked list if present
  12. You are required to complete this method*/
  13. void removeTheLoop(Node *head){
  14. //Handling base cases
  15. if(head==NULL)return;
  16. if( (head->next)==head){
  17. head->next=NULL;
  18. return;
  19. }
  20. Node *slow=head,*fast=head->next;
  21. // Detecting cycle
  22. bool cycle=false;
  23. while(fast){
  24. slow=slow->next;
  25. if(fast->next==NULL)
  26. break;
  27. fast=fast->next->next;
  28. if(slow==fast){
  29. cycle=true;
  30. break;
  31. }
  32. }
  33. if(cycle){
  34. slow=head;
  35. while(slow!=(fast->next)->next){
  36. slow=slow->next;
  37. fast=fast->next->next;
  38. }
  39. //Bomb difused successfully
  40. fast->next->next=NULL;
  41.  
  42.  
  43. }
  44.  
  45. }
Add Comment
Please, Sign In to add comment