educative.io

Not covered a case when K = list length

If K equals list length - no rotation is actually required. Proposed code is not handling such situation and I suppose it will throw an NRE exception or return null as a result depending on implementation.
For example list is 1-2-3, and K=3

Here we will have head as null:
for (int i = 0; i < skipLength - 1; i++)
lastNodeOfRotatedList = lastNodeOfRotatedList.next;
// ‘lastNodeOfRotatedList.next’ is pointing to the sub-list of ‘k’ ending nodes
head = lastNodeOfRotatedList.next;
lastNodeOfRotatedList.next = null;
return head;


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Solution Review: Problem Challenge 2 - Grokking the Coding Interview: Patterns for Coding Questions

Hello @Eugene_Polozhenkov,
If you enter the values 1,2 and 3 and set k=3 the code with just return the original list. You can test the see values out in the code. But this is being handled as it does no rotation.
Hope this helps

Sorry, this is my mistake. You are right.
I’ve missed that code is making a list circular before last loop.
Thanks!


Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Solution Review: Problem Challenge 2 - Grokking the Coding Interview: Patterns for Coding Questions