Skip to content

Commit 3cb9722

Browse files
committed
1
1 parent 7590d3f commit 3cb9722

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

notes/src/day3/lc203.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ impl ListNode {
3131
impl Solution {
3232
pub fn remove_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
3333
let mut head = Some(Box::new(ListNode { val: 0, next: head }));
34-
let mut a = &mut head;
34+
let mut a: &mut Option<Box<ListNode>> = &mut head;
3535
while a.as_deref_mut().unwrap().next.is_some() {
36+
// ^ Option<&mut ListNode>
37+
//
3638
let v = a.as_deref_mut().unwrap().next.as_deref().unwrap().val;
3739
if v == val {
3840
let mut b = a.as_deref_mut().unwrap().next.take();
@@ -48,7 +50,9 @@ impl Solution {
4850
}
4951
```
5052

51-
属实有点恶心了
53+
属实有点恶心了,看着太复杂了,这就是不用take的后果
54+
55+
5256

5357
## 学习感想
5458

@@ -135,4 +139,8 @@ impl Solution {
135139
res
136140
}
137141
}
138-
```
142+
```
143+
144+
链表的ownership还是非常容易理清楚的
145+
146+
一个Option不是owner没法直接unwrap,但是as_mut了之后可以随意unwrap,这也是容器穿透

0 commit comments

Comments
 (0)