# 反转链表 II

给你单链表的头指针 <code>head</code> 和两个整数 <code>left</code> 和 <code>right</code> ，其中 <code>left <= right</code> 。请你反转从位置 <code>left</code> 到位置 <code>right</code> 的链表节点，返回 <strong>反转后的链表</strong> 。<p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0092.Reverse%20Linked%20List%20II/images/rev2ex2.jpg" style="width: 542px; height: 222px;" /><pre><strong>输入：</strong>head = [1,2,3,4,5], left = 2, right = 4<strong><br />输出：</strong>[1,4,3,2,5]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = [5], left = 1, right = 1<strong><br />输出：</strong>[5]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点数目为 <code>n</code></li>	<li><code>1 <= n <= 500</code></li>	<li><code>-500 <= Node.val <= 500</code></li>	<li><code>1 <= left <= right <= n</code></li></ul><p> </p><p><strong>进阶：</strong> 你可以使用一趟扫描完成反转吗？</p>

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

```cpp
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
	int val;
	struct ListNode *next;
};
static struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
	int i;
	struct ListNode dummy;
	struct ListNode *prev = &dummy;
	prev->next = head;
	for (i = 1; i < m; i++)
	{
		prev = prev->next;
	}
	struct ListNode *p = prev->next;
	for (i = m; i < n; i++)
	{
		struct ListNode *q = p->next;
		__________________;
	}
	return dummy.next;
}
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		fprintf(stderr, "Usage: ./test m n 1 2 3...\n");
		exit(-1);
	}
	int i, count = argc - 3;
	struct ListNode dummy;
	struct ListNode *prev = &dummy;
	struct ListNode *p;
	for (i = 0; i < count; i++)
	{
		p = malloc(sizeof(*p));
		p->val = atoi(argv[i + 3]);
		p->next = NULL;
		prev->next = p;
		prev = p;
	}
	int m = atoi(argv[1]);
	int n = atoi(argv[2]);
	struct ListNode *head = reverseBetween(dummy.next, m, n);
	for (p = head; p != NULL; p = p->next)
	{
		printf("%d ", p->val);
	}
	printf("\n");
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
	int val;
	struct ListNode *next;
};
static struct ListNode *reverseBetween(struct ListNode *head, int m, int n)
{
	int i;
	struct ListNode dummy;
	struct ListNode *prev = &dummy;
	prev->next = head;
	for (i = 1; i < m; i++)
	{
		prev = prev->next;
	}
	struct ListNode *p = prev->next;
	for (i = m; i < n; i++)
	{
		struct ListNode *q = p->next;
		p->next = q->next;
		q->next = prev->next;
		prev->next = q;
	}
	return dummy.next;
}
int main(int argc, char **argv)
{
	if (argc < 3)
	{
		fprintf(stderr, "Usage: ./test m n 1 2 3...\n");
		exit(-1);
	}
	int i, count = argc - 3;
	struct ListNode dummy;
	struct ListNode *prev = &dummy;
	struct ListNode *p;
	for (i = 0; i < count; i++)
	{
		p = malloc(sizeof(*p));
		p->val = atoi(argv[i + 3]);
		p->next = NULL;
		prev->next = p;
		prev = p;
	}
	int m = atoi(argv[1]);
	int n = atoi(argv[2]);
	struct ListNode *head = reverseBetween(dummy.next, m, n);
	for (p = head; p != NULL; p = p->next)
	{
		printf("%d ", p->val);
	}
	printf("\n");
	return 0;
}
```

## 答案

```cpp
p->next = q->next;
q->next = prev->next;
prev->next = q;
```

## 选项

### A

```cpp
q->next = prev->next;
p->next = q->next;
prev->next = q;
```

### B

```cpp
prev->next = q;
p->next = q->next;
q->next = prev->next;
```

### C

```cpp
q->next = prev->next;
p->next = q->next;
prev->next = q;
```