Is the example 2 wrong ?
k = 8 here.
Kindly check it.
Yes, it seems that way. K should be 2 here
K=8 is correct.
As the list has only 5 element, so actually we have to rotate only 8%5 (=3) times. So we can say that k=3 in this case. Here is how we handled this in code:
rotations %= listLength; // no need to do rotations more than the length of the list
I feel example 2 is wrong too.
yes I agree that 8%5=3 so it actually rotate 3 times.
However see example 1 where k=3, after rotating the head is at node 4. Similarly in example 2 it should point to node 4 too after rotate 3 times.
Read the example again - it’s actually rotate to right so the answer is correct.
In example 2, shouldn’t it rotate by 3? (8%5)… The answer shown seems to be rotating by 2.
1 Like