Skip to content

Commit 4508eb5

Browse files
添加第65题
1 parent 54c8a48 commit 4508eb5

File tree

5 files changed

+137
-0
lines changed

5 files changed

+137
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* @lc app=leetcode id=83 lang=cpp
3+
*
4+
* [83] Remove Duplicates from Sorted List
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* struct ListNode {
11+
* int val;
12+
* ListNode *next;
13+
* ListNode() : val(0), next(nullptr) {}
14+
* ListNode(int x) : val(x), next(nullptr) {}
15+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
16+
* };
17+
*/
18+
class Solution {
19+
public:
20+
ListNode* deleteDuplicates(ListNode* head) {
21+
ListNode *dummy = new ListNode(-999);
22+
ListNode *d = dummy;
23+
dummy->next = head;
24+
ListNode *p = head;
25+
while (p != nullptr)
26+
{
27+
if (d->val != p->val)
28+
{
29+
d->next = p;
30+
d = p;
31+
}
32+
p = p->next;
33+
}
34+
d->next = p; // 最后一个重复的节点删除
35+
return dummy->next;
36+
}
37+
};
38+
// @lc code=end
39+
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# 删除链表中的重复元素
2+
3+
#### *Remove Duplicates from Sorted List*
4+
5+
存在一个按升序排列的链表,给你这个链表的头节点 `head` ,请你删除所有重复的元素,使每个元素 **只出现一次**
6+
7+
返回同样按升序排列的结果链表。
8+
9+
10+
英文题目:
11+
12+
Given the `head` of a sorted linked list, *delete all duplicates such that each element appears only once*. Return *the linked list **sorted** as well*.
13+
14+
15+
16+
**Constraints:**
17+
18+
- The number of nodes in the list is in the range `[0, 300]`.
19+
- `-100 <= Node.val <= 100`
20+
- The list is guaranteed to be **sorted** in ascending order.
21+
22+
**example 1**
23+
24+
![example 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/65.%20Remove%20Duplicates%20from%20Sorted%20List(Easy)/list1.jpeg)
25+
26+
```
27+
Input: head = [1,1,2]
28+
Output: [1,2]
29+
```
30+
31+
**example 2**
32+
33+
![example 1](https://github.com/SherlockUnknowEn/leetcode/blob/master/60-69/65.%20Remove%20Duplicates%20from%20Sorted%20List(Easy)/list2.jpeg)
34+
35+
```
36+
Input: head = [1,1,2,3,3]
37+
Output: [1,2,3]
38+
```
39+
40+
41+
42+
---
43+
44+
### 思路
45+
46+
1. `d`指针指向第一个元素,`p`指针遍历链表,过程中有
47+
2.`p->val == d->val`时,表明遇到了相同值,`d`不动,`p`继续遍历
48+
3.`p->val != d->val`时,令`d->next = p; d = d->next;`
49+
4. 注意处理最后一个重复节点即可
50+
51+
52+
### 代码
53+
```cpp
54+
55+
/*
56+
* @lc app=leetcode id=83 lang=cpp
57+
*
58+
* [83] Remove Duplicates from Sorted List
59+
*/
60+
61+
// @lc code=start
62+
/**
63+
* Definition for singly-linked list.
64+
* struct ListNode {
65+
* int val;
66+
* ListNode *next;
67+
* ListNode() : val(0), next(nullptr) {}
68+
* ListNode(int x) : val(x), next(nullptr) {}
69+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
70+
* };
71+
*/
72+
class Solution {
73+
public:
74+
ListNode* deleteDuplicates(ListNode* head) {
75+
ListNode *dummy = new ListNode(-999);
76+
ListNode *d = dummy;
77+
dummy->next = head;
78+
ListNode *p = head;
79+
while (p != nullptr)
80+
{
81+
if (d->val != p->val)
82+
{
83+
d->next = p;
84+
d = p;
85+
}
86+
p = p->next;
87+
}
88+
d->next = p; // 最后一个重复的节点删除
89+
return dummy->next;
90+
}
91+
};
92+
// @lc code=end
93+
94+
```
95+
96+
本题以及其它leetcode题目代码github地址: [github地址](https:github.com/SherlockUnknowEn/leetcode)
9.89 KB
Loading
15.1 KB
Loading

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@
6464

6565
##### 64. [92.Reverse Linked List II](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/64.%20Reverse%20Linked%20List%20II(Medium)) 反转链表 II
6666

67+
##### 65. [83.Remove Duplicates from Sorted List](https://github.com/SherlockUnknowEn/leetcode/tree/master/60-69/65.%20Remove%20Duplicates%20from%20Sorted%20List(Easy)) 删除排序链表中的重复元素
68+

0 commit comments

Comments
 (0)