File tree 3 files changed +106
-0
lines changed
solution/0000-0099/0083.Remove Duplicates from Sorted List
3 files changed +106
-0
lines changed Original file line number Diff line number Diff line change @@ -155,6 +155,43 @@ func deleteDuplicates(head *ListNode) *ListNode {
155
155
}
156
156
```
157
157
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
+
158
195
### ** ...**
159
196
160
197
```
Original file line number Diff line number Diff line change @@ -145,6 +145,43 @@ func deleteDuplicates(head *ListNode) *ListNode {
145
145
}
146
146
```
147
147
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
+
148
185
### ** ...**
149
186
150
187
```
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments