I am trying the following code challenge:
You are given a linked list of N nodes. The task is to remove the loop from the linked list, if present.
Note: C is the position of the node to which the last node is connected. If it is 0 then no loop.
Example 1:
Input:
N = 3 value[] = {1,3,4} C = 2Output: 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.
Example 2:
Input:
N = 4 value[] = {1,8,3,4} C = 0Output: 1
Explanation: N = 4 and x = 0, which means lastNode->next = NULL, thus the Linked list does not contains any loop.
Your Task:
Your task is to complete the function
removeLoop(). The only argument of the function is head pointer of the linked list. Simply remove the loop in the list (if present) without disconnecting any nodes from the list. The driver code will print 1 if your code is correct.Expected time complexity : O(n)
Expected auxiliary space : O(1)
Constraints:
1 <= N <= 104
My Code:
'''
class Node:
def __init__(self,val):
self.next=None
self.data=val
'''
def removeLoop(head):
slow = fast = head
while fast!=None and fast.next!=None:
slow = slow.next
fast = fast.next.next
if slow==fast:
x = slow
temp = head
while x.next!=temp.next:
x = x.next
temp = temp.next
x.next = None
return head
I am getting a Runtime Error. Can anyone tell me why?