Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 27, 2025. It is now read-only.

Commit 2caf2df

Browse files
committed
83. Remove Duplicates from Sorted List
1 parent cd743be commit 2caf2df

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# 83. Remove Duplicates from Sorted List
2+
3+
### 2017年04月06日
4+
5+
Given a sorted linked list, delete all duplicates such that each element appear only *once*.
6+
7+
For example,
8+
Given `1->1->2`, return `1->2`.
9+
Given `1->1->2->3->3`, return `1->2->3`.
10+
11+
12+
13+
# Solution
14+
15+
```swift
16+
/**
17+
* Definition for singly-linked list.
18+
* public class ListNode {
19+
* public var val: Int
20+
* public var next: ListNode?
21+
* public init(_ val: Int) {
22+
* self.val = val
23+
* self.next = nil
24+
* }
25+
* }
26+
*/
27+
class Solution {
28+
func deleteDuplicates(_ head: ListNode?) -> ListNode? {
29+
guard var current = head else { return head }
30+
while let next = current.next {
31+
if current.val == next.val {
32+
current.next = next.next
33+
} else {
34+
current = next
35+
}
36+
}
37+
return head
38+
}
39+
}
40+
```
41+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /