# K 个一组翻转链表

<p>给你一个链表，每 <em>k </em>个节点一组进行翻转，请你返回翻转后的链表。</p><p><em>k </em>是一个正整数，它的值小于或等于链表的长度。</p><p>如果节点总数不是 <em>k </em>的整数倍，那么请将最后剩余的节点保持原有顺序。</p><p><strong>进阶：</strong></p><ul>	<li>你可以设计一个只使用常数额外空间的算法来解决此问题吗？</li>	<li><strong>你不能只是单纯的改变节点内部的值</strong>，而是需要实际进行节点交换。</li></ul><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/73746d9eacbf48526f3aa7f3051d929a.jpeg" style="width: 542px; height: 222px;" /><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 2<strong><br />输出：</strong>[2,1,4,3,5]</pre><p><strong>示例 2：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/202a5b66c99b0dba832f105fae9a47ce.jpeg" style="width: 542px; height: 222px;" /><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 3<strong><br />输出：</strong>[3,2,1,4,5]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>head = [1,2,3,4,5], k = 1<strong><br />输出：</strong>[1,2,3,4,5]</pre><p><strong>示例 4：</strong></p><pre><strong>输入：</strong>head = [1], k = 1<strong><br />输出：</strong>[1]</pre><ul></ul><p><strong>提示：</strong></p><ul>	<li>列表中节点的数量在范围 <code>sz</code> 内</li>	<li><code>1 <= sz <= 5000</code></li>	<li><code>0 <= Node.val <= 1000</code></li>	<li><code>1 <= k <= sz</code></li></ul>

以下程序实现了这一功能，请你填补空白处内容：

```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 reverseKGroup(self, head, k):
		if head is None:
			return None
		index = 0
		lead, last = 0, 0
		pos = head
		temp = ListNode(-1)
		temp.next = head
		head = temp
		start = head
		_________________;
		return head.next
	def reverseList(self, head, end):
		pos = head.next
		last = end
		next_start = pos
		while pos != end:
			head.next = pos
			last_pos = pos
			pos = pos.next
			last_pos.next = last
			last = last_pos
		return next_start
# %%
l = LinkList()
head = [1,2,3,4, 5]
l1 = l.initList(head)
s = Solution()
print(l.convert_list(s.reverseKGroup(l1, k = 2)))
```

## 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 reverseKGroup(self, head, k):
		if head is None:
			return None
		index = 0
		lead, last = 0, 0
		pos = head
		temp = ListNode(-1)
		temp.next = head
		head = temp
		start = head
		while pos is not None:
			if index % k == k - 1:
				last = pos.next
				start = self.reverseList(start, last)
				pos = start
			pos = pos.next
			index += 1
		return head.next
	def reverseList(self, head, end):
		pos = head.next
		last = end
		next_start = pos
		while pos != end:
			head.next = pos
			last_pos = pos
			pos = pos.next
			last_pos.next = last
			last = last_pos
		return next_start
# %%
l = LinkList()
head = [1,2,3,4, 5]
l1 = l.initList(head)
s = Solution()
print(l.convert_list(s.reverseKGroup(l1, k = 2)))
```

## 答案

```python
while pos is not None:
	if index % k == k - 1:
		last = pos.next
		start = self.reverseList(start, last)
		pos = start
	pos = pos.next
	index += 1
```

## 选项

### A

```python
while pos is not None:
	if index % k == k + 1:
		last = pos.next
		start = self.reverseList(start, last)
		pos = start
	pos = pos.next
	index += 1
```

### B

```python
while pos is not None:
	if index % k == k:
		last = pos.next
		start = self.reverseList(start, last)
		pos = start
	pos = pos.next
	index += 1
```

### C

```python
while pos is not None:
	if index // k == k + 1:
		last = pos.next
		start = self.reverseList(start, last)
		pos = start
	pos = pos.next
	index += 1
```