@@ -853,21 +853,62 @@ <h2 id="学习感想-5"><a class="header" href="#学习感想-5">学习感想</a
853
853
pub fn remove_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
854
854
let mut dummyHead = Box::new(ListNode::new(0));
855
855
dummyHead.next = head;
856
- let mut cur = dummyHead.as_mut();
856
+ let mut cur: &mut ListNode = dummyHead.as_mut();
857
857
// 使用take()替换std::men::replace(&mut node.next, None)达到相同的效果,并且更普遍易读
858
858
while let Some(nxt) = cur.next.take() {
859
859
if nxt.val == val {
860
860
cur.next = nxt.next;
861
861
} else {
862
862
cur.next = Some(nxt);
863
- cur = cur.next.as_mut().unwrap();
863
+ cur = cur.next.as_mut().unwrap(); // coercion
864
+ // ^ Option<Box<ListNode>>
865
+ // ^ Option<&mut Box<ListNode>>
866
+ // ^ &mut Box<ListNode>
867
+ // deref coerce -> & mut ListNode
864
868
}
865
869
}
866
870
dummyHead.next
867
871
}
868
872
}
869
873
< span class ="boring "> }</ span > </ code > </ pre > </ pre >
870
874
< p > 向这位老哥学习,使用take,管它用不用,先取下来再说。并且 先把option刨了</ p >
875
+ < pre > < pre class ="playground "> < code class ="language-rust "> < span class ="boring "> #![allow(unused)]
876
+ </ span >
877
+ < span class ="boring "> fn main() {
878
+ </ span > < span class ="boring "> struct Solution {}
879
+ </ span > < span class ="boring ">
880
+ </ span > < span class ="boring "> // Definition for singly-linked list.
881
+ </ span > < span class ="boring "> #[derive(PartialEq, Eq, Clone, Debug)]
882
+ </ span > < span class ="boring "> pub struct ListNode {
883
+ </ span > < span class ="boring "> pub val: i32,
884
+ </ span > < span class ="boring "> pub next: Option<Box<ListNode>>
885
+ </ span > < span class ="boring "> }
886
+ </ span > < span class ="boring ">
887
+ </ span > < span class ="boring "> impl ListNode {
888
+ </ span > < span class ="boring "> #[inline]
889
+ </ span > < span class ="boring "> fn new(val: i32) -> Self {
890
+ </ span > < span class ="boring "> ListNode {
891
+ </ span > < span class ="boring "> next: None,
892
+ </ span > < span class ="boring "> val
893
+ </ span > < span class ="boring "> }
894
+ </ span > < span class ="boring "> }
895
+ </ span > < span class ="boring "> }
896
+ </ span >
897
+ impl Solution {
898
+ pub fn remove_elements(mut head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
899
+ let mut res: Option<Box<ListNode>> = None;
900
+ let mut ptr: &mut Option<Box<ListNode>> = &mut res;
901
+ while let Some(mut x) = head {
902
+ head = x.next.take();
903
+ if x.val != val {
904
+ *ptr = Some(x);
905
+ ptr = &mut ptr.as_mut().unwrap().next;
906
+ }
907
+ }
908
+ res
909
+ }
910
+ }
911
+ < span class ="boring "> }</ span > </ code > </ pre > </ pre >
871
912
< div style ="break-before: page; page-break-before: always; "> </ div > < h1 id ="707-设计链表 "> < a class ="header " href ="#707-设计链表 "> 707. 设计链表</ a > </ h1 >
872
913
< h2 id ="题目描述-6 "> < a class ="header " href ="#题目描述-6 "> 题目描述</ a > </ h2 >
873
914
< p > 你可以选择使用单链表或者双链表,设计并实现自己的链表。</ p >
0 commit comments