Skip to content

Commit a8aa9cd

Browse files
committed
feat: add solutions to leetcode problems: No.0082, No.0083
1 parent da2bd34 commit a8aa9cd

File tree

13 files changed

+435
-82
lines changed

13 files changed

+435
-82
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
- [两数相加](/solution/0000-0099/0002.Add%20Two%20Numbers/README.md)
7777
- [从尾到头打印链表](/lcof/面试题06.%20从尾到头打印链表/README.md)
7878
- [删除链表的节点](/lcof/面试题18.%20删除链表的节点/README.md)
79+
- [删除排序链表中的重复元素](/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/README.md)
80+
- [删除排序链表中的重复元素 II](/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/README.md)
7981
- [移除链表元素](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README.md)
8082
- [链表中倒数第 k 个节点](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README.md)
8183
- [两两交换链表中的节点](/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/README.md)

README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
7474

7575
- [Add Two Numbers](/solution/0000-0099/0002.Add%20Two%20Numbers/README_EN.md)
7676
- [Delete Node in a Linked List](/solution/0200-0299/0237.Delete%20Node%20in%20a%20Linked%20List/README_EN.md)
77+
- [Remove Duplicates from Sorted List](/solution/0000-0099/0083.Remove%20Duplicates%20from%20Sorted%20List/README_EN.md)
78+
- [Remove Duplicates from Sorted List II](/solution/0000-0099/0082.Remove%20Duplicates%20from%20Sorted%20List%20II/README_EN.md)
7779
- [Remove Linked List Elements](/solution/0200-0299/0203.Remove%20Linked%20List%20Elements/README_EN.md)
7880
- [Kth Node From End of List](/lcci/02.02.Kth%20Node%20From%20End%20of%20List/README_EN.md)
7981
- [Swap Nodes in Pairs](/solution/0000-0099/0024.Swap%20Nodes%20in%20Pairs/README_EN.md)

solution/0000-0099/0082.Remove Duplicates from Sorted List II/README.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,90 @@
2929
<!-- 这里可写当前语言的特殊实现逻辑 -->
3030

3131
```python
32-
32+
# Definition for singly-linked list.
33+
# class ListNode:
34+
# def __init__(self, val=0, next=None):
35+
# self.val = val
36+
# self.next = next
37+
class Solution:
38+
def deleteDuplicates(self, head: ListNode) -> ListNode:
39+
dummy = ListNode(-1, head)
40+
cur = dummy
41+
while cur.next and cur.next.next:
42+
if cur.next.val == cur.next.next.val:
43+
val = cur.next.val
44+
while cur.next and cur.next.val == val:
45+
cur.next = cur.next.next
46+
else:
47+
cur = cur.next
48+
return dummy.next
3349
```
3450

3551
### **Java**
3652

3753
<!-- 这里可写当前语言的特殊实现逻辑 -->
3854

3955
```java
56+
/**
57+
* Definition for singly-linked list.
58+
* public class ListNode {
59+
* int val;
60+
* ListNode next;
61+
* ListNode() {}
62+
* ListNode(int val) { this.val = val; }
63+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
64+
* }
65+
*/
66+
class Solution {
67+
public ListNode deleteDuplicates(ListNode head) {
68+
ListNode dummy = new ListNode(-1, head);
69+
ListNode cur = dummy;
70+
while (cur.next != null && cur.next.next != null) {
71+
if (cur.next.val == cur.next.next.val) {
72+
int val = cur.next.val;
73+
while (cur.next != null && cur.next.val == val) {
74+
cur.next = cur.next.next;
75+
}
76+
} else {
77+
cur = cur.next;
78+
}
79+
}
80+
return dummy.next;
81+
}
82+
}
83+
```
4084

85+
### **C++**
86+
87+
```cpp
88+
/**
89+
* Definition for singly-linked list.
90+
* struct ListNode {
91+
* int val;
92+
* ListNode *next;
93+
* ListNode() : val(0), next(nullptr) {}
94+
* ListNode(int x) : val(x), next(nullptr) {}
95+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
96+
* };
97+
*/
98+
class Solution {
99+
public:
100+
ListNode* deleteDuplicates(ListNode* head) {
101+
ListNode* dummy = new ListNode(-1, head);
102+
ListNode* cur = dummy;
103+
while (cur->next != nullptr && cur->next->next != nullptr) {
104+
if (cur->next->val == cur->next->next->val) {
105+
int val = cur->next->val;
106+
while (cur->next != nullptr && cur->next->val == val) {
107+
cur->next = cur->next->next;
108+
}
109+
} else {
110+
cur = cur->next;
111+
}
112+
}
113+
return dummy->next;
114+
}
115+
};
41116
```
42117
43118
### **...**

solution/0000-0099/0082.Remove Duplicates from Sorted List II/README_EN.md

+76-1
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,88 @@
2929
### **Python3**
3030

3131
```python
32-
32+
# Definition for singly-linked list.
33+
# class ListNode:
34+
# def __init__(self, val=0, next=None):
35+
# self.val = val
36+
# self.next = next
37+
class Solution:
38+
def deleteDuplicates(self, head: ListNode) -> ListNode:
39+
dummy = ListNode(-1, head)
40+
cur = dummy
41+
while cur.next and cur.next.next:
42+
if cur.next.val == cur.next.next.val:
43+
val = cur.next.val
44+
while cur.next and cur.next.val == val:
45+
cur.next = cur.next.next
46+
else:
47+
cur = cur.next
48+
return dummy.next
3349
```
3450

3551
### **Java**
3652

3753
```java
54+
/**
55+
* Definition for singly-linked list.
56+
* public class ListNode {
57+
* int val;
58+
* ListNode next;
59+
* ListNode() {}
60+
* ListNode(int val) { this.val = val; }
61+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
62+
* }
63+
*/
64+
class Solution {
65+
public ListNode deleteDuplicates(ListNode head) {
66+
ListNode dummy = new ListNode(-1, head);
67+
ListNode cur = dummy;
68+
while (cur.next != null && cur.next.next != null) {
69+
if (cur.next.val == cur.next.next.val) {
70+
int val = cur.next.val;
71+
while (cur.next != null && cur.next.val == val) {
72+
cur.next = cur.next.next;
73+
}
74+
} else {
75+
cur = cur.next;
76+
}
77+
}
78+
return dummy.next;
79+
}
80+
}
81+
```
3882

83+
### **C++**
84+
85+
```cpp
86+
/**
87+
* Definition for singly-linked list.
88+
* struct ListNode {
89+
* int val;
90+
* ListNode *next;
91+
* ListNode() : val(0), next(nullptr) {}
92+
* ListNode(int x) : val(x), next(nullptr) {}
93+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
94+
* };
95+
*/
96+
class Solution {
97+
public:
98+
ListNode* deleteDuplicates(ListNode* head) {
99+
ListNode* dummy = new ListNode(-1, head);
100+
ListNode* cur = dummy;
101+
while (cur->next != nullptr && cur->next->next != nullptr) {
102+
if (cur->next->val == cur->next->next->val) {
103+
int val = cur->next->val;
104+
while (cur->next != nullptr && cur->next->val == val) {
105+
cur->next = cur->next->next;
106+
}
107+
} else {
108+
cur = cur->next;
109+
}
110+
}
111+
return dummy->next;
112+
}
113+
};
39114
```
40115
41116
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* deleteDuplicates(ListNode* head) {
14+
ListNode* dummy = new ListNode(-1, head);
15+
ListNode* cur = dummy;
16+
while (cur->next != nullptr && cur->next->next != nullptr) {
17+
if (cur->next->val == cur->next->next->val) {
18+
int val = cur->next->val;
19+
while (cur->next != nullptr && cur->next->val == val) {
20+
cur->next = cur->next->next;
21+
}
22+
} else {
23+
cur = cur->next;
24+
}
25+
}
26+
return dummy->next;
27+
}
28+
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
111
class Solution {
212
public ListNode deleteDuplicates(ListNode head) {
3-
if (head == null || head.next == null) {
4-
return head;
5-
}
6-
7-
if (head.val == head.next.val) {
8-
if (head.next.next == null) {
9-
return null;
10-
}
11-
if (head.val == head.next.next.val) {
12-
return deleteDuplicates(head.next);
13+
ListNode dummy = new ListNode(-1, head);
14+
ListNode cur = dummy;
15+
while (cur.next != null && cur.next.next != null) {
16+
if (cur.next.val == cur.next.next.val) {
17+
int val = cur.next.val;
18+
while (cur.next != null && cur.next.val == val) {
19+
cur.next = cur.next.next;
20+
}
21+
} else {
22+
cur = cur.next;
1323
}
14-
return deleteDuplicates(head.next.next);
1524
}
16-
head.next = deleteDuplicates(head.next);
17-
return head;
25+
return dummy.next;
1826
}
1927
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
# Definition for singly-linked list.
22
# class ListNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.next = None
6-
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
76
class Solution:
8-
def deleteDuplicates(self, head):
9-
"""
10-
:type head: ListNode
11-
:rtype: ListNode
12-
"""
13-
if head == None:
14-
return None
15-
list0=list()
16-
i=head
17-
while i:
18-
list0.append(i.val)
19-
i=i.next
20-
list1=list0.copy()
21-
for i in list0:
22-
c=list0.count(i)
23-
if c != 1:
24-
while 1:
25-
try:
26-
list1.remove(i)
27-
except:
28-
break
29-
30-
new_listnode=ListNode(0)
31-
j=new_listnode
32-
for v in list1:
33-
new_listnode.next=ListNode(v)
34-
new_listnode=new_listnode.next
35-
return j.next
7+
def deleteDuplicates(self, head: ListNode) -> ListNode:
8+
dummy = ListNode(-1, head)
9+
cur = dummy
10+
while cur.next and cur.next.next:
11+
if cur.next.val == cur.next.next.val:
12+
val = cur.next.val
13+
while cur.next and cur.next.val == val:
14+
cur.next = cur.next.next
15+
else:
16+
cur = cur.next
17+
return dummy.next

0 commit comments

Comments
 (0)