Remove loop in Linked List
PROBLEM :
Given a linked list, remove the loop in it if present. The task is to complete the function removeTheLoop which takes only one argument the head of the linked list . The function removes the loop in the linked list if present.
Input:
The first line of input will contain an integer T denoting the no of test cases . Then T test cases follow. Each test case contains 3 lines . The first line of each test case contains an integer N denoting the no of nodes of the linked list . In the next line are N space separated values denoting the values of the linked list. The next line after it contains an integer x denoting that the last node of the linked list pointing to the xth node thus resulting in cycle.
Output:
Your task is to remove the cycle if present output for each test case will be 1 if the loop is successfully removed from the linked list else 0.
Constraints:
1<=T<=50
1<=N<=300
Example(To be used only for expected output) :
Input:
2
3
1 3 4
2
4
1 8 3 4
0
Output:
1
1
Explanation:
In the first test case N = 3
The linked list with nodes N = 3 is given. here x=2 which means last node is connected with xth node of linked list. Therefore, there exists a loop.
In the second test where N = 4 and x = 0, which means lastNode->next = NULL, thus the Linked list does not contains any loop.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of linked list is the following
struct node
{
int data;
node* next;
};*/
/*The function removes the loop
from the linked list if present
You are required to complete this method*/
node* find_collide_point(node *) ;
void removeTheLoop(node *list)
{
if(list==NULL)
return ;
node *mearge ;
mearge=find_collide_point(list) ;
if(mearge==NULL)
return ;
node *temp ;
temp=list ;
while(temp->next!=mearge->next)
{
temp=temp->next ;
mearge=mearge->next ;
}
mearge->next=NULL ;
}
node* find_collide_point(node *list)
{
node *one,*two ;
one=list ;
two=list ;
while(one!=two&&(two!=NULL&&two->next!=NULL))
{
one=one->next ;
two=two->next->next ; ;
}
if(two==NULL||two->next==NULL)
return NULL ;
return one ;
}
---------------------------------------------------------------------------------
Given a linked list, remove the loop in it if present. The task is to complete the function removeTheLoop which takes only one argument the head of the linked list . The function removes the loop in the linked list if present.
Input:
The first line of input will contain an integer T denoting the no of test cases . Then T test cases follow. Each test case contains 3 lines . The first line of each test case contains an integer N denoting the no of nodes of the linked list . In the next line are N space separated values denoting the values of the linked list. The next line after it contains an integer x denoting that the last node of the linked list pointing to the xth node thus resulting in cycle.
Output:
Your task is to remove the cycle if present output for each test case will be 1 if the loop is successfully removed from the linked list else 0.
Constraints:
1<=T<=50
1<=N<=300
Example(To be used only for expected output) :
Input:
2
3
1 3 4
2
4
1 8 3 4
0
Output:
1
1
Explanation:
In the first test case N = 3
The linked list with nodes N = 3 is given. here x=2 which means last node is connected with xth node of linked list. Therefore, there exists a loop.
In the second test where N = 4 and x = 0, which means lastNode->next = NULL, thus the Linked list does not contains any loop.
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
The structure of linked list is the following
struct node
{
int data;
node* next;
};*/
/*The function removes the loop
from the linked list if present
You are required to complete this method*/
node* find_collide_point(node *) ;
void removeTheLoop(node *list)
{
if(list==NULL)
return ;
node *mearge ;
mearge=find_collide_point(list) ;
if(mearge==NULL)
return ;
node *temp ;
temp=list ;
while(temp->next!=mearge->next)
{
temp=temp->next ;
mearge=mearge->next ;
}
mearge->next=NULL ;
}
node* find_collide_point(node *list)
{
node *one,*two ;
one=list ;
two=list ;
while(one!=two&&(two!=NULL&&two->next!=NULL))
{
one=one->next ;
two=two->next->next ; ;
}
if(two==NULL||two->next==NULL)
return NULL ;
return one ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment