# 旋转链表

<p>给你一个链表的头节点 <code>head</code> ，旋转链表，将链表每个节点向右移动 <code>k</code><em> </em>个位置。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/c0d8ae472058ab78d44488d3d18b7d34.jpeg" style="width: 600px; height: 254px;" /><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 2<strong><br />输出：</strong>[4,5,1,2,3]</pre><p><strong>示例 2：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/b36376b20ad1074341ffc8bca9a32eca.jpeg" style="width: 472px; height: 542px;" /><pre><strong>输入：</strong>head = [0,1,2], k = 4<strong><br />输出：</strong>[2,0,1]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点的数目在范围 <code>[0, 500]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>0 <= k <= 2 * 10<sup>9</sup></code></li></ul>

## template

```python
class ListNode(object):
	def __init__(self, x):
		self.val = x
		self.next = None
class LinkList:
	def __init__(self):
		self.head=None
	def initList(self, data):
		self.head = ListNode(data[0])
		r=self.head
		p = self.head
		for i in data[1:]:
			node = ListNode(i)
			p.next = node
			p = p.next
		return r
	def	convert_list(self,head):
		ret = []
		if head == None:
			return
		node = head
		while node != None:
			ret.append(node.val)
			node = node.next
		return ret
class Solution(object):
	def rotateRight(self, head, k):
		"""
		:type head: ListNode
		:type k: int
		:rtype: ListNode
		"""
		if not head or k == 0:
			return head
		slow = fast = head
		length = 1
		while k and fast.next:
			fast = fast.next
			length += 1
			k -= 1
		if k != 0:
			k = (k + length - 1) % length 
			return self.rotateRight(head, k)
		else:
			while fast.next:
				fast = fast.next
				slow = slow.next
			return self.rotate(head, fast, slow)
	def rotate(self, head, fast, slow):
		fast.next = head
		head = slow.next
		slow.next = None
		return head
# %%
l = LinkList()
list1 = [0,1,2]
k = 4
l1 = l.initList(list1)
s = Solution()
print(l.convert_list(s.rotateRight(l1, k)))
```

## 答案

```python

```

## 选项

### A

```python

```

### B

```python

```

### C

```python

```