-
Notifications
You must be signed in to change notification settings - Fork 161
/
rotate-list.md
22 lines (18 loc) · 912 Bytes
/
rotate-list.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<p>Given a linked list, rotate the list to the right by <em>k</em> places, where <em>k</em> is non-negative.</p>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input:</strong> 1->2->3->4->5->NULL, k = 2
<strong>Output:</strong> 4->5->1->2->3->NULL
<strong>Explanation:</strong>
rotate 1 steps to the right: 5->1->2->3->4->NULL
rotate 2 steps to the right: 4->5->1->2->3->NULL
</pre>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input:</strong> 0->1->2->NULL, k = 4
<strong>Output:</strong> <code>2->0->1->NULL</code>
<strong>Explanation:</strong>
rotate 1 steps to the right: 2->0->1->NULL
rotate 2 steps to the right: 1->2->0->NULL
rotate 3 steps to the right: <code>0->1->2->NULL</code>
rotate 4 steps to the right: <code>2->0->1->NULL</code></pre>