Remove loop in Linked List
Submissions: 26298   Accuracy:


  Difficulty: Medium   Marks: 4

You are given a linked list of N nodes. You need to remove the loop if present.
Note: You may use the detectloop function that is already present for loop detection.

Input Format:
First line of input contains number of testcases T. T testcases follow. For each testcase, first line of input contains length N of the linked list and next line contains N data of the linked list.  The third line contains the position of the node(from head) to which the last node will get connected. If it is 0 then there is no loop.

For each testcase, in a new line, 1 will be printed if loop is removed(correct answer) else 0 will be printed(for wrong answer).

User Task:
You don't have to read the input, or print the output. Just complete the function removeTheLoop() which has only argument as head reference of the linked list. If you complete this function in correct way and loop gets removed, the answer will be 1.

1 <= T <= 50
1 <= N <= 300

1 3 4
1 8 3 4


Testcase 1: 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. 

Testcase 2: N = 4 and x = 0, which means lastNode->next = NULL, thus the Linked list does not contains any loop.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: Shubham Joshi 1

Need help with your code? Please use, generate link and share the link here.

to report an issue on this page.