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

Commit 41d49dc

Browse files
authored
feat: add rust solution to lc problem: No.0083 (#1710)
1 parent d117485 commit 41d49dc

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,43 @@ func deleteDuplicates(head *ListNode) *ListNode {
155155
}
156156
```
157157

158+
### **Rust**
159+
160+
```rust
161+
// Definition for singly-linked list.
162+
// #[derive(PartialEq, Eq, Clone, Debug)]
163+
// pub struct ListNode {
164+
// pub val: i32,
165+
// pub next: Option<Box<ListNode>>
166+
// }
167+
//
168+
// impl ListNode {
169+
// #[inline]
170+
// fn new(val: i32) -> Self {
171+
// ListNode {
172+
// next: None,
173+
// val
174+
// }
175+
// }
176+
// }
177+
impl Solution {
178+
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
179+
let mut dummy = Some(Box::new(ListNode::new(i32::MAX)));
180+
let mut p = &mut dummy;
181+
182+
let mut current = head;
183+
while let Some(mut node) = current {
184+
current = node.next.take();
185+
if p.as_mut().unwrap().val != node.val {
186+
p.as_mut().unwrap().next = Some(node);
187+
p = &mut p.as_mut().unwrap().next;
188+
}
189+
}
190+
dummy.unwrap().next
191+
}
192+
}
193+
```
194+
158195
### **...**
159196

160197
```

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,43 @@ func deleteDuplicates(head *ListNode) *ListNode {
145145
}
146146
```
147147

148+
### **Rust**
149+
150+
```rust
151+
// Definition for singly-linked list.
152+
// #[derive(PartialEq, Eq, Clone, Debug)]
153+
// pub struct ListNode {
154+
// pub val: i32,
155+
// pub next: Option<Box<ListNode>>
156+
// }
157+
//
158+
// impl ListNode {
159+
// #[inline]
160+
// fn new(val: i32) -> Self {
161+
// ListNode {
162+
// next: None,
163+
// val
164+
// }
165+
// }
166+
// }
167+
impl Solution {
168+
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
169+
let mut dummy = Some(Box::new(ListNode::new(i32::MAX)));
170+
let mut p = &mut dummy;
171+
172+
let mut current = head;
173+
while let Some(mut node) = current {
174+
current = node.next.take();
175+
if p.as_mut().unwrap().val != node.val {
176+
p.as_mut().unwrap().next = Some(node);
177+
p = &mut p.as_mut().unwrap().next;
178+
}
179+
}
180+
dummy.unwrap().next
181+
}
182+
}
183+
```
184+
148185
### **...**
149186

150187
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn delete_duplicates(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
19+
let mut dummy = Some(Box::new(ListNode::new(i32::MAX)));
20+
let mut p = &mut dummy;
21+
22+
let mut current = head;
23+
while let Some(mut node) = current {
24+
current = node.next.take();
25+
if p.as_mut().unwrap().val != node.val {
26+
p.as_mut().unwrap().next = Some(node);
27+
p = &mut p.as_mut().unwrap().next;
28+
}
29+
}
30+
dummy.unwrap().next
31+
}
32+
}

0 commit comments

Comments
(0)

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