https://ide.codingblocks.com/s/148402 my code is giving runtime error .
please check.
Linked list K-Append Problem
@Ajitverma1503 you were not checking for the cases where k is greater than n. In line number 80 you have to pass k%n instead of k.
And since we needed the value of n later on, I also changed the while loop to for loop, as while loop would decrease the value of n to 0.
Hope this helps.
Try this function for appending the k elements :
is my code wrong ? I donβt understand what are you saying ?
I am asking you to try the given function for appending the k elements since there is a little error in your logic.
Hello @Ajitverma1503,
Following are the reasons for run-time error:
The logic of your append() function is correct but it will fail for the cases
-
when k=0
Reason:
curr is NULL for this case. -
when k>=n
Example:
7
1 2 2 1 8 5 6
8
Expected Output:
6 1 2 2 1 8 5
Your Output:
1 2 2 1 8 5 6
Observation:
The required output is the same as that of k=1.
Similarly,
for k=7 the output is the same as that of k=0.
for k=9 the output is the same as that of k=2.
Solution:
k = k mod n;
this would limit the value of k within the range [0,n-1]
I have modified your code:
Hope, this would help.
Give a like if you are satisfied.
Thank you for explaination