LeetCode(Problem of Day)

LeetCode(Problem of Day)


**Initial Thought(Just in One Go):-

1. As the question, stats rotate list by k place.

2. Now vary first thought is , pick up the last element in each of the rotation 

  and put in the start.

3. After performing k no of times these step , we gets our job done.

4. But if we see the time complexity , is O(K*L) where K is no of time rotation perform 

  and L is the last node of list , every time we have to go the last element and gets our 

  task completed.

5. Taking about the Space Complexity , here we are not using any space , so space is O(1).

**Thoughts after some Paper Pen work:-

1. As we have to rotate K time, L is the length of List.

2. When we do prepare the test case , do some stuff , where we get the original list in     multiple order of L value , if L=4 and K=13 , then we get original list when K value  is 4 ,8, 12 in rotation, so if K>=L then update K=K%L. Or we can do same for K<L also     because both case covered in that.  

3. Point last node to the head of list.

4. Some how reach (length of list -K) position then break the connection , and point next vary node to the null value, which gets our task done.

**Algorithm Design:-

1. Count the Length of the Linked list.

2. Point Last node to the head of list.

3. Make the next node of (L-k) position as the head node of Linked List.

4. Then make (L-k) position node, node.next point to null.

**Edge Case:-

Their is only three case case are their :-

1. First if the node==null.

2. If their is only one node , as node.next==null.

3. If we have to perform 0 rotation , as k==0.

--> IN ALL THREE CASE :- return the head , as the original list given.**

**Time and Space:-

1. Time Complexity is O(L) (for finding length of list) + O(L-(L%K)) (for moving L-k position  to break the connection).

2. Space Complexity is O(1) , as we are not using any extra space.

Code:-

No alt text provided for this image


To view or add a comment, sign in

More articles by Avnish Kumar 🇮🇳 🇮🇳 🇮🇳

  • LeetCode(Problem Of Day)

    LeetCode(Problem Of Day)

    Initials thinking for the problem :- 1.Just noobs thinking(common person) by seeing the statement , reverse the list L1…

Insights from the community

Others also viewed

Explore topics