# 删除排序链表中的重复元素 II

<p>存在一个按升序排列的链表，给你这个链表的头节点 <code>head</code> ，请你删除链表中所有存在数字重复情况的节点，只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/5b9243e41528c71944e0f9436d8c249f.jpeg" style="width: 500px; height: 142px;" /><pre><strong>输入：</strong>head = [1,2,3,3,4,4,5]<strong><br />输出：</strong>[1,2,5]</pre><p><strong>示例 2：</strong></p><img alt="" src="https://img-service.csdnimg.cn/img_convert/f43c3f1fdb51738ae73ac8543c9b4e9b.jpeg" style="width: 500px; height: 205px;" /><pre><strong>输入：</strong>head = [1,1,1,2,3]<strong><br />输出：</strong>[2,3]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>	<li><code>-100 <= Node.val <= 100</code></li>	<li>题目数据保证链表已经按升序排列</li></ul>

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

```cpp
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
	int val;
	struct ListNode *next;
};
struct ListNode *deleteDuplicates(struct ListNode *head)
{
	struct ListNode dummy;
	struct ListNode *p, *q, *prev;
	prev = &dummy;
	dummy.next = head;
	p = q = head;
	while (p != NULL)
	{
		_____________________;
	}
	return dummy.next;
}
int main(int argc, char **argv)
{
	int i;
	struct ListNode *head = NULL;
	struct ListNode *prev = NULL;
	struct ListNode *p;
	for (i = 0; i < argc - 1; i++)
	{
		p = malloc(sizeof(*p));
		p->val = atoi(argv[i + 1]);
		p->next = NULL;
		if (head == NULL)
		{
			head = p;
			prev = head;
		}
		else
		{
			prev->next = p;
			prev = p;
		}
	}
	p = deleteDuplicates(head);
	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 *deleteDuplicates(struct ListNode *head)
{
	struct ListNode dummy;
	struct ListNode *p, *q, *prev;
	prev = &dummy;
	dummy.next = head;
	p = q = head;
	while (p != NULL)
	{
		while (q != NULL && q->val == p->val)
		{
			q = q->next;
		}
		if (p->next == q)
		{
			prev = p;
		}
		else
		{
			prev->next = q;
		}
		p = q;
	}
	return dummy.next;
}
int main(int argc, char **argv)
{
	int i;
	struct ListNode *head = NULL;
	struct ListNode *prev = NULL;
	struct ListNode *p;
	for (i = 0; i < argc - 1; i++)
	{
		p = malloc(sizeof(*p));
		p->val = atoi(argv[i + 1]);
		p->next = NULL;
		if (head == NULL)
		{
			head = p;
			prev = head;
		}
		else
		{
			prev->next = p;
			prev = p;
		}
	}
	p = deleteDuplicates(head);
	while (p != NULL)
	{
		printf("%d ", p->val);
		p = p->next;
	}
	printf("\n");
	return 0;
}
```

## 答案

```cpp
while (q != NULL && q->val == p->val)
{
	q = q->next;
}
if (p->next == q)
{
	prev = p;
}
else
{
	prev->next = q;
}
p = q;
```

## 选项

### A

```cpp
while (q != NULL && q->val == p->val)
{
	q = q->next;
}
if (p->next == q)
{
	prev = p;
}
else
{
	prev->next = q;
}
```

### B

```cpp
while (q != NULL && q->val == p->val)
{
	q = q->next;
}
if (p->next == q)
{
	prev = p;
}
p = q;
```

### C

```cpp
while (q != NULL && q->val == p->val)
{
	q = q->next;
}
if (p->next == q)
{
	prev->next = q;
}
p = q;
```