@@ -808,8 +808,10 @@ <h2 id="解题思路-5"><a class="header" href="#解题思路-5">解题思路</a
808
808
impl Solution {
809
809
pub fn remove_elements(head: Option<Box<ListNode>>, val: i32) -> Option<Box<ListNode>> {
810
810
let mut head = Some(Box::new(ListNode { val: 0, next: head }));
811
- let mut a = &mut head;
811
+ let mut a: &mut Option<Box<ListNode>> = &mut head;
812
812
while a.as_deref_mut().unwrap().next.is_some() {
813
+ // ^ Option<&mut ListNode>
814
+ //
813
815
let v = a.as_deref_mut().unwrap().next.as_deref().unwrap().val;
814
816
if v == val {
815
817
let mut b = a.as_deref_mut().unwrap().next.take();
@@ -824,7 +826,7 @@ <h2 id="解题思路-5"><a class="header" href="#解题思路-5">解题思路</a
824
826
}
825
827
}
826
828
< span class ="boring "> }</ span > </ code > </ pre > </ pre >
827
- < p > 属实有点恶心了</ p >
829
+ < p > 属实有点恶心了,看着太复杂了,这就是不用take的后果 </ p >
828
830
< h2 id ="学习感想-5 "> < a class ="header " href ="#学习感想-5 "> 学习感想</ a > </ h2 >
829
831
< pre > < pre class ="playground "> < code class ="language-rust "> < span class ="boring "> #![allow(unused)]
830
832
</ span >
@@ -909,6 +911,8 @@ <h2 id="学习感想-5"><a class="header" href="#学习感想-5">学习感想</a
909
911
}
910
912
}
911
913
< span class ="boring "> }</ span > </ code > </ pre > </ pre >
914
+ < p > 链表的ownership还是非常容易理清楚的</ p >
915
+ < p > 一个Option不是owner没法直接unwrap,但是as_mut了之后可以随意unwrap,这也是容器穿透</ p >
912
916
< div style ="break-before: page; page-break-before: always; "> </ div > < h1 id ="707-设计链表 "> < a class ="header " href ="#707-设计链表 "> 707. 设计链表</ a > </ h1 >
913
917
< h2 id ="题目描述-6 "> < a class ="header " href ="#题目描述-6 "> 题目描述</ a > </ h2 >
914
918
< p > 你可以选择使用单链表或者双链表,设计并实现自己的链表。</ p >
@@ -1017,6 +1021,74 @@ <h2 id="解题思路-6"><a class="header" href="#解题思路-6">解题思路</a
1017
1021
< span class ="boring "> }</ span > </ code > </ pre > </ pre >
1018
1022
< h2 id ="学习感想-6 "> < a class ="header " href ="#学习感想-6 "> 学习感想</ a > </ h2 >
1019
1023
< p > 也没啥好说的,rust只要写出来,基本是对的,没有用take的形式,而是全部去除了option用ref</ p >
1024
+ < pre > < pre class ="playground "> < code class ="language-rust "> < span class ="boring "> #![allow(unused)]
1025
+ </ span > < span class ="boring "> fn main() {
1026
+ </ span > struct MyLinkedList {
1027
+ head: Option<Box<ListNode>>,
1028
+ cnt: i32,
1029
+ }
1030
+
1031
+ struct ListNode {
1032
+ val: i32,
1033
+ next: Option<Box<ListNode>>,
1034
+ }
1035
+
1036
+ /**
1037
+ * `&self` means the method takes an immutable reference.
1038
+ * If you need a mutable reference, change it to `&mut self` instead.
1039
+ */
1040
+ impl MyLinkedList {
1041
+
1042
+ fn new() -> Self {
1043
+ Self {
1044
+ head: None,
1045
+ cnt: 0i32,
1046
+ }
1047
+ }
1048
+
1049
+ fn get(&self, mut index: i32) -> i32 {
1050
+ if index >= self.cnt { return -1i32 }
1051
+ let mut ptr: & Box<ListNode> = self.head.as_ref().unwrap();
1052
+ while index > 0i32 {
1053
+ ptr = ptr.next.as_ref().unwrap();
1054
+ index -= 1i32;
1055
+ }
1056
+ ptr.val
1057
+ }
1058
+
1059
+ fn add_at_head(&mut self, val: i32) {
1060
+ self.add_at_index(0i32, val);
1061
+ }
1062
+
1063
+ fn add_at_tail(&mut self, val: i32) {
1064
+ self.add_at_index(self.cnt, val);
1065
+ }
1066
+
1067
+ fn add_at_index(&mut self, mut index: i32, val: i32) {
1068
+ if index > self.cnt { return }
1069
+ let mut ptr: &mut Option<Box<ListNode>> = &mut self.head;
1070
+ while index > 0i32 {
1071
+ ptr = &mut ptr.as_mut().unwrap().next;
1072
+ index -= 1i32;
1073
+ }
1074
+ self.cnt += 1i32;
1075
+ *ptr = Some(Box::new(ListNode { val: val, next: ptr.take() }))
1076
+ }
1077
+
1078
+ fn delete_at_index(&mut self, mut index: i32) {
1079
+ if index >= self.cnt { return }
1080
+ let mut ptr: &mut Option<Box<ListNode>> = &mut self.head;
1081
+ while index > 0i32 {
1082
+ ptr = &mut ptr.as_mut().unwrap().next;
1083
+ index -= 1i32;
1084
+ }
1085
+ let nxt: Option<Box<ListNode>> = ptr.take().unwrap().next.take();
1086
+ *ptr = nxt;
1087
+ self.cnt -= 1i32;
1088
+ }
1089
+ }
1090
+
1091
+ < span class ="boring "> }</ span > </ code > </ pre > </ pre >
1020
1092
< div style ="break-before: page; page-break-before: always; "> </ div > < h1 id ="206-反转链表 "> < a class ="header " href ="#206-反转链表 "> 206. 反转链表</ a > </ h1 >
1021
1093
< h2 id ="题目描述-7 "> < a class ="header " href ="#题目描述-7 "> 题目描述</ a > </ h2 >
1022
1094
< p > 给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。</ p >
@@ -1061,6 +1133,41 @@ <h2 id="解题思路-7"><a class="header" href="#解题思路-7">解题思路</a
1061
1133
< span class ="boring "> }</ span > </ code > </ pre > </ pre >
1062
1134
< h2 id ="学习感想-7 "> < a class ="header " href ="#学习感想-7 "> 学习感想</ a > </ h2 >
1063
1135
< p > 所以我这个算是什么方式</ p >
1136
+ < pre > < pre class ="playground "> < code class ="language-rust "> < span class ="boring "> #![allow(unused)]
1137
+ </ span >
1138
+
1139
+ < span class ="boring "> fn main() {
1140
+ </ span > struct Solution {}
1141
+
1142
+ // Definition for singly-linked list.
1143
+ #[derive(PartialEq, Eq, Clone, Debug)]
1144
+ pub struct ListNode {
1145
+ pub val: i32,
1146
+ pub next: Option<Box<ListNode>>
1147
+ }
1148
+
1149
+ impl ListNode {
1150
+ #[inline]
1151
+ fn new(val: i32) -> Self {
1152
+ ListNode {
1153
+ next: None,
1154
+ val
1155
+ }
1156
+ }
1157
+ }
1158
+
1159
+ impl Solution {
1160
+ pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
1161
+ let mut res: Option<Box<ListNode>> = None;
1162
+ while let Some(mut x) = head {
1163
+ head = x.next.take();
1164
+ x.next = res;
1165
+ res = Some(x);
1166
+ }
1167
+ res
1168
+ }
1169
+ }
1170
+ < span class ="boring "> }</ span > </ code > </ pre > </ pre >
1064
1171
< div style ="break-before: page; page-break-before: always; "> </ div > < h1 id ="第二章-链表part02 "> < a class ="header " href ="#第二章-链表part02 "> 第二章 链表part02</ a > </ h1 >
1065
1172
< p > ● day 1 任务以及具体安排:https://docs.qq.com/doc/DUG9UR2ZUc3BjRUdY
1066
1173
● day 2 任务以及具体安排:https://docs.qq.com/doc/DUGRwWXNOVEpyaVpG
0 commit comments