Why we need mod k by length?


  • 2
    Y

    I'm wondering why we need execute k %= (length of list). Should we return the ordinary list when k >= length? This is not a circular list.

    Do anyone know what does the "rotate" mean? Thank you very much!


  • 1
    J

    Yes, this is not a circular list. But, in the question, when you rotate the list, it should be treated as if it is circular. That being said, the value of k could be larger than the length of list. And I believe that's the reason of taking mod of length. Hope this help.


  • 1

    K%=length is done to save time , making the program efficient .

    Assume length of list = 26 , k = 26 .

    if we literally rotate the list 26 times , we would again regain the same list . So doing k = k%26 , gives k = 0. Hence , no rotation is done and list head is returned as it is .

    Assume length of list = 26 , k = 28 .

    So here , k=28 is same as k = k%26 , k = 2 , in disguise . So hence , only 2 shifts of elements in the list gives the answer.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.