Back to course home
0% completed
Solution: Problem Challenge 2: Rotate a LinkedList
Problem Statement
Given the head of a Singly LinkedList and a number ‘k’, rotate the LinkedList to the right by ‘k’ nodes.
Constraints:
- The number of nodes in the list is in the range
[0, 500]
. -100 <= Node.val <= 100
- 0 <= k <= 2 * 10^9
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. 2
.....
.....
.....
Like the course? Get enrolled and start learning!