Linked List K Append Question

https://ide.codingblocks.com/s/62725

@Khushboo Please Resolve it.

TESTCASE #1: correct (Time: 0 s)
TESTCASE #2: run-error (Time: 0 s)
TESTCASE #3: run-error (Time: 0 s)
TESTCASE #4: run-error (Time: 0 s)

Hi Monika, pls notice that it is given in the question that k can be greater than or even equal to n.
Pls take this point into consideration. You just have to make a small change in k as soon as you input it.
After adding that 1 statement your code will work fine except 1 corner case for which you have to put a condition and check and deal with it seperately.

I have given you enough hints. Hope this helps :slight_smile:

How can k is greater than n?

@proRram Can you please tell?

ok lets say we have a list of size n=7 and k = 3

original list is: 1 2 3 4 5 6 7

after processing it the list will be :

5 6 7 1 2 3 4

what happens if k = 7
in this case we have to put all the last k=7 elements in front. Notice that this will result into the original list itself.
i.e. for all k=n and also k=0 output is same as input.

now what happens if k = 9.
we simply put as many no. of elements in the front of list which are possible
which in this case is 7 because list has maximum 7 elements.

Let me name this shifting of all n elements to obtain the list to be same as original as task 1.

now after we have shifted seven elements, i.e. done with task 1; we are left with 9 - 7 = 2 elements to be shifted. i.e. we now need to shift 2 more elements.

so the list becomes:

6 7 1 2 3 4 5

also lets take an example where k = 17.

now lets do task1 here i.e. shift maximum possible elements i.e. all 7 elements to front
we are now left with 17 - 7 = 10
again we shift maximum no. of elements, i.e. we perform task 1 and now we are left with 10-7 = 3
elements.
now we shift he last 3 elements to front so the final output will be:

5 6 7 1 2 3 4

this is what happens if k is larger than n.

Now what you need to figure out is how to skip task 1 for all cases where k >= n. This is because shifting elements to reach a configuration which you already are at affects time complexity of the program. you just need to find the no. of element that will be left after maximum possible no. of task1s performed.

Hint: you have to use an operator.

Hope this helps :slight_smile:

1 Like

Thank you :star_struck:

1 Like