solution.md 2.2 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
# 删除排序链表中的重复元素 II

<p>存在一个按升序排列的链表,给你这个链表的头节点 <code>head</code> ,请你删除链表中所有存在数字重复情况的节点,只保留原始链表中 <strong>没有重复出现</strong><em> </em>的数字。</p><p>返回同样按升序排列的结果链表。</p><p> </p><p><strong>示例 1:</strong></p><img alt="" src="https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist1.jpg" 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://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/images/linkedlist2.jpg" 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>

## 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

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```