for the above question i m submitting this function but it is giving runtime error
Practicing linked list
This problem is similar to finding the Nth node from the end of the linked list.
Sample Input 2 :
1 2 3 4 5 6 7 8 9 10 -1
7
5
You may assume end of the linked list as position k, ie. the point where loop begins.
Here position k is at node 7.
So 5th node from the end of linked list (assuming node 7 is the end) is 5.
Your Floyd algo for cycle detection is correct.
Error is due to the next part where you have to print the Nth node towards the head of the linked.
Dry run with this case and check that part.
in he second part i m takind kth node as the last node and thn appling the same algo which we used for kth node frm end by taking n
Is your doubt resolved now… or still you have any issue?
runtime error is coming
still giving run time error
I checked the corrected(whose link i have shared) code on codezen.
It was running well for the sample inputs and did not give any runtime error.
Please check again. Earlier your code was not running because you did not put a break statement after a loop was detected.
this is the code i am submitting
// Following is the node structure
/**************
class ListNode{
public:
int data;
ListNode* next;
};
ListNode* newListNode(int data){
ListNode *temp = new ListNode;
temp->data = data;
temp->next = NULL;
return temp;
}
***************/
ListNode* FindNode(ListNode* head, int N) {
/*Write your code here.
*Don’t write main().
*Don’t take input, it is passed as function argument.
*Don’t print output.
*Taking input and printing output is handled automatically.
*/
ListNode*slow = head;
ListNode*fast = head;
ListNode*temp = NULL;
ListNode*k = NULL;
if(fast == NULL && fast->next == NULL){
return NULL;
}
while(fast != NULL && fast->next != NULL){
fast = fast->next->next;
slow = slow->next;
if(fast == NULL && fast->next == NULL){
return NULL;
}
if(slow == fast){
slow = head;
temp = fast;
}
while(temp->next!=slow->next){
temp = temp->next;
slow = slow->next;
}
k = slow->next;
break;
}
ListNode*slow1 = head;
ListNode*fast1 = head;
for(int i=1;i<N;i++){
fast1 = fast1->next;
}
while(fast1!=k && fast1->next!=k){
fast1 = fast1->next;
slow1 = slow1->next;
}
return slow1;
}
Check the code in the link i provided above.
if(slow == fast)
{
slow = head;
temp = fast;
//remove this }
while(temp->next!=slow->next){
temp = temp->next;
slow = slow->next;
}
k = slow->next;
break;
}
All these lines must be within the if condition.
BECAUSE you perform these statements only when you detect a loop
Add a special case to check that if a node at distance N from the point where loop starts actually exists or not.
Consider
1 2 3 4 5 6 7 8 9 10 -1
7
8
Your o/p:
3
Expected output:
NULL
I added an extra condition https://ide.codingblocks.com/s/292538
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.



