Solution Review: Problem Challenge 2
We'll cover the following
Rotate a LinkedList (medium) #
Given the head of a Singly LinkedList and a number ‘k’, rotate the LinkedList to the right by ‘k’ nodes.
Solution #
Another way of defining the rotation is to take the sub-list of ‘k’ ending nodes of the LinkedList and connect them to the beginning. Other than that we have to do three more things:
- Connect the last node of the LinkedList to the head, because the list will have a different tail after the rotation.
- The new head of the LinkedList will be the node at the beginning of the sublist.
- The node right before the start of sub-list will be the new tail of the rotated LinkedList.
Code #
Here is what our algorithm will look like: