Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding
Ninjas X Naukri.com

Problem of the day

You are given a linked list having ** ‘n’** nodes and an integer

You have to rotate the linked list to the right by ‘k’ positions .

```
Input: linked list = [1 2 3 4] , k = 2
Output: 3 4 1 2
Explanation:
We have to rotate the given linked list to the right 2 times. After rotating it to the right once it becomes 4->1->2->3. After rotating it to the right again, it becomes 3->4->1->2.
```

Detailed explanation

```
6
1 2 3 4 5 6
2
```

```
5 6 1 2 3 4
```

```
For the first test case, after 1st clockwise rotation the modified linked list will be : 6->1->2->3->4->5
After, 2nd clockwise rotated the modified linked list will be : 5->6->1->2->3->4.
Thus the output is: 5 6 1 2 3 4.
```

```
3
3 6 9
0
```

```
3 6 9
```

```
In this test case, ‘k’ is 0 therefore there will be no rotation, so the linked list remains unchanged.
```

```
Try to do this in O(n).
```

```
1 <= n <= 10^5
0 <= Node.data <= 10^9
0 <= k <= 10^5
Time Limit: 1 sec
```