# 分隔链表

<p>给你一个链表的头节点 <code>head</code> 和一个特定值<em> </em><code>x</code> ，请你对链表进行分隔，使得所有 <strong>小于</strong> <code>x</code> 的节点都出现在 <strong>大于或等于</strong> <code>x</code> 的节点之前。</p><p>你应当 <strong>保留</strong> 两个分区中每个节点的初始相对位置。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/54f1177fe2cc4fddce6e1a5553659bee.jpeg" style="width: 662px; height: 222px;" /><pre><strong>输入：</strong>head = [1,4,3,2,5,2], x = 3<strong><br />输出</strong>：[1,2,2,4,3,5]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>head = [2,1], x = 2<strong><br />输出</strong>：[1,2]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点的数目在范围 <code>[0, 200]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li><code>-200 <= x <= 200</code></li></ul>

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

```cpp
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
	int val;
	struct ListNode *next;
};
struct ListNode *partition(struct ListNode *head, int x)
{
	struct ListNode dummy;
	struct ListNode *prev1 = &dummy, *pivot;
	dummy.next = head;
	for (pivot = head; pivot != NULL; pivot = pivot->next)
	{
		if (pivot->val >= x)
		{
			break;
		}
		prev1 = pivot;
	}
	struct ListNode *p = pivot->next;
	struct ListNode *prev2 = pivot;
	while (p != NULL)
	{
		if (p->val < x)
		{
			__________________;
		}
		else
		{
			prev2 = p;
			p = p->next;
		}
	}
	return dummy.next;
}
int main(int argc, char **argv)
{
	if (argc < 2)
	{
		fprintf(stderr, "Usage: ./test target n1 n2 n3...\n");
		exit(-1);
	}
	int i, target = atoi(argv[1]);
	struct ListNode *head = NULL;
	struct ListNode *prev = NULL;
	struct ListNode *p;
	for (i = 0; i < argc - 2; i++)
	{
		p = malloc(sizeof(*p));
		p->val = atoi(argv[i + 2]);
		p->next = NULL;
		if (head == NULL)
		{
			head = p;
			prev = head;
		}
		else
		{
			prev->next = p;
			prev = p;
		}
	}
	p = partition(head, target);
	while (p != NULL)
	{
		printf("%d ", p->val);
		p = p->next;
	}
	printf("\n");
	return 0;
}
```

## template

```cpp
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
	int val;
	struct ListNode *next;
};
struct ListNode *partition(struct ListNode *head, int x)
{
	struct ListNode dummy;
	struct ListNode *prev1 = &dummy, *pivot;
	dummy.next = head;
	for (pivot = head; pivot != NULL; pivot = pivot->next)
	{
		if (pivot->val >= x)
		{
			break;
		}
		prev1 = pivot;
	}
	struct ListNode *p = pivot->next;
	struct ListNode *prev2 = pivot;
	while (p != NULL)
	{
		if (p->val < x)
		{
			prev2->next = p->next;
			p->next = prev1->next;
			prev1->next = p;
			prev1 = p;
			p = prev2->next;
		}
		else
		{
			prev2 = p;
			p = p->next;
		}
	}
	return dummy.next;
}
int main(int argc, char **argv)
{
	if (argc < 2)
	{
		fprintf(stderr, "Usage: ./test target n1 n2 n3...\n");
		exit(-1);
	}
	int i, target = atoi(argv[1]);
	struct ListNode *head = NULL;
	struct ListNode *prev = NULL;
	struct ListNode *p;
	for (i = 0; i < argc - 2; i++)
	{
		p = malloc(sizeof(*p));
		p->val = atoi(argv[i + 2]);
		p->next = NULL;
		if (head == NULL)
		{
			head = p;
			prev = head;
		}
		else
		{
			prev->next = p;
			prev = p;
		}
	}
	p = partition(head, target);
	while (p != NULL)
	{
		printf("%d ", p->val);
		p = p->next;
	}
	printf("\n");
	return 0;
}
```

## 答案

```cpp
prev2->next = p->next;
p->next = prev1->next;
prev1->next = p;
prev1 = p;
p = prev2->next;
```

## 选项

### A

```cpp
p->next = prev1->next;
prev1->next = p;
prev2->next = p->next;
prev1 = p;
p = prev2->next;
```

### B

```cpp
p->next = prev1->next;
prev1->next = p;
prev2->next = p->next;
p = prev2->next;
prev1 = p;
```

### C

```cpp
p->next = prev1->next;
prev2->next = p->next;
prev1->next = p;
p = prev2->next;
prev1 = p;
```