0%

remove-duplicates-from-sorted-list

题目描述

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。

题解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
ListNode* deleteDuplicates(ListNode* head) {
if (!head || !head->next)return head;
int value = head->val;
ListNode* cur = head;
while (cur&&cur->next)
{
value = cur->val;
if (cur->next->val == value)
{
ListNode* tmp = cur->next;
while (tmp&&tmp->val == value)
tmp = tmp->next;
cur->next = tmp;
}
cur = cur->next;
}
return head;
}