-
Notifications
You must be signed in to change notification settings - Fork 0
/
0083.删除排序链表中的重复元素.cpp
62 lines (61 loc) · 1.24 KB
/
0083.删除排序链表中的重复元素.cpp
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
/*
* @lc app=leetcode.cn id=83 lang=cpp
*
* [83] 删除排序链表中的重复元素
*
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/description/
*
* algorithms
* Easy (47.31%)
* Likes: 230
* Dislikes: 0
* Total Accepted: 59.7K
* Total Submissions: 123.5K
* Testcase Example: '[1,1,2]'
*
* 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
*
* 示例 1:
*
* 输入: 1->1->2
* 输出: 1->2
*
*
* 示例 2:
*
* 输入: 1->1->2->3->3
* 输出: 1->2->3
*
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head)
{
if (head == nullptr) {
return head;
}
ListNode* p1 = head;
ListNode* p2 = p1->next;
while (p1 != nullptr) {
p2 = p1->next;
while (p2 != nullptr && p2->val == p1->val) {
p2 = p2->next;
}
if (p2 != p1->next) {
p1->next = p2;
}
p1 = p2;
}
return head;
}
};
// @lc code=end