Skip to content

Commit d109613

Browse files
authored
feat: update solutions to lc problems: No.83,95 (#2167)
1 parent faa3a7c commit d109613

File tree

14 files changed

+630
-551
lines changed

14 files changed

+630
-551
lines changed

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

+76-24
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
<!-- 这里可写通用的实现逻辑 -->
4040

41+
**方法一:一次遍历**
42+
43+
我们用一个指针 $cur$ 来遍历链表。如果当前 $cur$ 与 $cur.next$ 对应的元素相同,我们就将 $cur$ 的 $next$ 指针指向 $cur$ 的下下个节点。否则,说明链表中 $cur$ 对应的元素是不重复的,因此可以将 $cur$ 指针移动到下一个节点。
44+
45+
遍历结束后,返回链表的头节点即可。
46+
47+
时间复杂度 $O(n)$,其中 $n$ 是链表的长度。空间复杂度 $O(1)$。
48+
4149
<!-- tabs:start -->
4250

4351
### **Python3**
@@ -61,25 +69,6 @@ class Solution:
6169
return head
6270
```
6371

64-
```python
65-
# Definition for singly-linked list.
66-
# class ListNode:
67-
# def __init__(self, val=0, next=None):
68-
# self.val = val
69-
# self.next = next
70-
class Solution:
71-
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
72-
dummy = ListNode(1000)
73-
cur = dummy
74-
while head:
75-
if head.val != cur.val:
76-
cur.next = head
77-
cur = cur.next
78-
head = head.next
79-
cur.next = None
80-
return dummy.next
81-
```
82-
8372
### **Java**
8473

8574
<!-- 这里可写当前语言的特殊实现逻辑 -->
@@ -142,19 +131,53 @@ public:
142131
### **Go**
143132
144133
```go
134+
/**
135+
* Definition for singly-linked list.
136+
* type ListNode struct {
137+
* Val int
138+
* Next *ListNode
139+
* }
140+
*/
145141
func deleteDuplicates(head *ListNode) *ListNode {
146-
current := head
147-
for current != nil && current.Next != nil {
148-
if current.Val == current.Next.Val {
149-
current.Next = current.Next.Next
142+
cur := head
143+
for cur != nil && cur.Next != nil {
144+
if cur.Val == cur.Next.Val {
145+
cur.Next = cur.Next.Next
150146
} else {
151-
current = current.Next
147+
cur = cur.Next
152148
}
153149
}
154150
return head
155151
}
156152
```
157153

154+
### **JavaScript**
155+
156+
```js
157+
/**
158+
* Definition for singly-linked list.
159+
* function ListNode(val) {
160+
* this.val = val;
161+
* this.next = null;
162+
* }
163+
*/
164+
/**
165+
* @param {ListNode} head
166+
* @return {ListNode}
167+
*/
168+
var deleteDuplicates = function (head) {
169+
let cur = head;
170+
while (cur && cur.next) {
171+
if (cur.next.val === cur.val) {
172+
cur.next = cur.next.next;
173+
} else {
174+
cur = cur.next;
175+
}
176+
}
177+
return head;
178+
};
179+
```
180+
158181
### **Rust**
159182

160183
```rust
@@ -192,6 +215,35 @@ impl Solution {
192215
}
193216
```
194217

218+
### **C#**
219+
220+
```cs
221+
/**
222+
* Definition for singly-linked list.
223+
* public class ListNode {
224+
* public int val;
225+
* public ListNode next;
226+
* public ListNode(int val=0, ListNode next=null) {
227+
* this.val = val;
228+
* this.next = next;
229+
* }
230+
* }
231+
*/
232+
public class Solution {
233+
public ListNode DeleteDuplicates(ListNode head) {
234+
ListNode cur = head;
235+
while (cur != null && cur.next != null) {
236+
if (cur.val == cur.next.val) {
237+
cur.next = cur.next.next;
238+
} else {
239+
cur = cur.next;
240+
}
241+
}
242+
return head;
243+
}
244+
}
245+
```
246+
195247
### **...**
196248

197249
```

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

+76-24
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232

3333
## Solutions
3434

35+
**Solution 1: Single Pass**
36+
37+
We use a pointer $cur$ to traverse the linked list. If the element corresponding to the current $cur$ is the same as the element corresponding to $cur.next$, we set the $next$ pointer of $cur$ to point to the next node of $cur.next$. Otherwise, it means that the element corresponding to $cur$ in the linked list is not duplicated, so we can move the $cur$ pointer to the next node.
38+
39+
After the traversal ends, return the head node of the linked list.
40+
41+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
42+
3543
<!-- tabs:start -->
3644

3745
### **Python3**
@@ -53,25 +61,6 @@ class Solution:
5361
return head
5462
```
5563

56-
```python
57-
# Definition for singly-linked list.
58-
# class ListNode:
59-
# def __init__(self, val=0, next=None):
60-
# self.val = val
61-
# self.next = next
62-
class Solution:
63-
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
64-
dummy = ListNode(1000)
65-
cur = dummy
66-
while head:
67-
if head.val != cur.val:
68-
cur.next = head
69-
cur = cur.next
70-
head = head.next
71-
cur.next = None
72-
return dummy.next
73-
```
74-
7564
### **Java**
7665

7766
```java
@@ -132,19 +121,53 @@ public:
132121
### **Go**
133122
134123
```go
124+
/**
125+
* Definition for singly-linked list.
126+
* type ListNode struct {
127+
* Val int
128+
* Next *ListNode
129+
* }
130+
*/
135131
func deleteDuplicates(head *ListNode) *ListNode {
136-
current := head
137-
for current != nil && current.Next != nil {
138-
if current.Val == current.Next.Val {
139-
current.Next = current.Next.Next
132+
cur := head
133+
for cur != nil && cur.Next != nil {
134+
if cur.Val == cur.Next.Val {
135+
cur.Next = cur.Next.Next
140136
} else {
141-
current = current.Next
137+
cur = cur.Next
142138
}
143139
}
144140
return head
145141
}
146142
```
147143

144+
### **JavaScript**
145+
146+
```js
147+
/**
148+
* Definition for singly-linked list.
149+
* function ListNode(val) {
150+
* this.val = val;
151+
* this.next = null;
152+
* }
153+
*/
154+
/**
155+
* @param {ListNode} head
156+
* @return {ListNode}
157+
*/
158+
var deleteDuplicates = function (head) {
159+
let cur = head;
160+
while (cur && cur.next) {
161+
if (cur.next.val === cur.val) {
162+
cur.next = cur.next.next;
163+
} else {
164+
cur = cur.next;
165+
}
166+
}
167+
return head;
168+
};
169+
```
170+
148171
### **Rust**
149172

150173
```rust
@@ -182,6 +205,35 @@ impl Solution {
182205
}
183206
```
184207

208+
### **C#**
209+
210+
```cs
211+
/**
212+
* Definition for singly-linked list.
213+
* public class ListNode {
214+
* public int val;
215+
* public ListNode next;
216+
* public ListNode(int val=0, ListNode next=null) {
217+
* this.val = val;
218+
* this.next = next;
219+
* }
220+
* }
221+
*/
222+
public class Solution {
223+
public ListNode DeleteDuplicates(ListNode head) {
224+
ListNode cur = head;
225+
while (cur != null && cur.next != null) {
226+
if (cur.val == cur.next.val) {
227+
cur.next = cur.next.next;
228+
} else {
229+
cur = cur.next;
230+
}
231+
}
232+
return head;
233+
}
234+
}
235+
```
236+
185237
### **...**
186238

187239
```
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
public class Solution {
2-
public ListNode DeleteDuplicates(ListNode head) {
3-
if (head == null) return null;
4-
var last = head;
5-
var current = head.next;
6-
while (current != null)
7-
{
8-
if (current.val != last.val)
9-
{
10-
last.next = current;
11-
last = current;
12-
}
13-
else
14-
{
15-
last.next = null;
16-
}
17-
current = current.next;
18-
}
19-
return head;
20-
}
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public ListNode DeleteDuplicates(ListNode head) {
14+
ListNode cur = head;
15+
while (cur != null && cur.next != null) {
16+
if (cur.val == cur.next.val) {
17+
cur.next = cur.next.next;
18+
} else {
19+
cur = cur.next;
20+
}
21+
}
22+
return head;
23+
}
2124
}

solution/0000-0099/0083.Remove Duplicates from Sorted List/Solution.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
18
func deleteDuplicates(head *ListNode) *ListNode {
2-
current := head
3-
for current != nil && current.Next != nil {
4-
if current.Val == current.Next.Val {
5-
current.Next = current.Next.Next
9+
cur := head
10+
for cur != nil && cur.Next != nil {
11+
if cur.Val == cur.Next.Val {
12+
cur.Next = cur.Next.Next
613
} else {
7-
current = current.Next
14+
cur = cur.Next
815
}
916
}
1017
return head

solution/0000-0099/0083.Remove Duplicates from Sorted List/Solution.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
* @return {ListNode}
1111
*/
1212
var deleteDuplicates = function (head) {
13-
var p = head;
14-
while (p && p.next) {
15-
if (p.val == p.next.val) {
16-
p.next = p.next.next;
13+
let cur = head;
14+
while (cur && cur.next) {
15+
if (cur.next.val === cur.val) {
16+
cur.next = cur.next.next;
1717
} else {
18-
p = p.next;
18+
cur = cur.next;
1919
}
2020
}
2121
return head;
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Definition for singly-linked list.
2-
# class ListNode:
3-
# def __init__(self, val=0, next=None):
4-
# self.val = val
5-
# self.next = next
6-
class Solution:
7-
def deleteDuplicates(self, head: ListNode) -> ListNode:
8-
cur = head
9-
while cur and cur.next:
10-
if cur.val == cur.next.val:
11-
cur.next = cur.next.next
12-
else:
13-
cur = cur.next
14-
return head
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, val=0, next=None):
4+
# self.val = val
5+
# self.next = next
6+
class Solution:
7+
def deleteDuplicates(self, head: Optional[ListNode]) -> Optional[ListNode]:
8+
cur = head
9+
while cur and cur.next:
10+
if cur.val == cur.next.val:
11+
cur.next = cur.next.next
12+
else:
13+
cur = cur.next
14+
return head

0 commit comments

Comments
 (0)