8000 1 · Ainevsia/Leetcode-Rust@968aab7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 968aab7

Browse files
committed
1
1 parent 3cb9722 commit 968aab7

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

notes/src/day3/lc206.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,40 @@ impl Solution {
4747

4848
## 学习感想
4949

50-
所以我这个算是什么方式
50+
所以我这个算是什么方式
51+
52+
53+
```rust
54+
55+
56+
struct Solution {}
57+
58+
// Definition for singly-linked list.
59+
#[derive(PartialEq, Eq, Clone, Debug)]
60+
pub struct ListNode {
61+
pub val: i32,
62+
pub next: Option<Box<ListNode>>
63+
}
64+
65+
impl ListNode {
66+
#[inline]
67+
fn new(val: i32) -> Self {
68+
ListNode {
69+
next: None,
70+
val
71+
}
72+
}
73+
}
74+
75+
impl Solution {
76+
pub fn reverse_list(mut head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
77+
let mut res: Option<Box<ListNode>> = None;
78+
while let Some(mut x) = head {
79+
head = x.next.take();
80+
x.next = res;
81+
res = Some(x);
82+
}
83+
res
84+
}
85+
}
86+
```

notes/src/day3/lc707.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,82 @@ impl MyLinkedList {
114114

115115
## 学习感想
116116

117-
也没啥好说的,rust只要写出来,基本是对的,没有用take的形式,而是全部去除了option用ref
117+
也没啥好说的,rust只要写出来,基本是对的,没有用take的形式,而是全部去除了option用ref
118+
119+
120+
```rust
121+
struct MyLinkedList {
122+
head: Option<Box<ListNode>>,
123+
cnt: i32,
124+
}
125+
126+
struct ListNode {
127+
val: i32,
128+
next: Option<Box<ListNode>>,
129+
}
130+
131+
/**
132+
* `&self` means the method takes an immutable reference.
133+
* If you need a mutable reference, change it to `&mut self` instead.
134+
*/
135+
impl MyLinkedList {
136+
137+
fn new() -> Self {
138+
Self {
139+
head: None,
140+
cnt: 0i32,
141+
}
142+
}
143+
144+
fn get(&self, mut index: i32) -> i32 {
145+
if index >= self.cnt { return -1i32 }
146+
let mut ptr: & Box<ListNode> = self.head.as_ref().unwrap();
147+
while index > 0i32 {
148+
ptr = ptr.next.as_ref().unwrap();
149+
index -= 1i32;
150+
}
151+
ptr.val
152+
}
153+
154+
fn add_at_head(&mut self, val: i32) {
155+
self.add_at_index(0i32, val);
156+
}
157+
158+
fn add_at_tail(&mut self, val: i32) {
159+
self.add_at_index(self.cnt, val);
160+
}
161+
162+
fn add_at_index(&mut self, mut index: i32, val: i32) {
163+
if index > self.cnt { return }
164+
let mut ptr: &mut Option<Box<ListNode>> = &mut self.head;
165+
while index > 0i32 {
166+
ptr = &mut ptr.as_mut().unwrap().next;
167+
index -= 1i32;
168+
}
169+
self.cnt += 1i32;
170+
*ptr = Some(Box::new(ListNode { val: val, next: ptr.take() }))
171+
}
172+
173+
fn delete_at_index(&mut self, mut index: i32) {
174+
if index >= self.cnt { return }
175+
let mut ptr: &mut Option<Box<ListNode>> = &mut self.head;
176+
while index > 0i32 {
177+
ptr = &mut ptr.as_mut().unwrap().next;
178+
index -= 1i32;
179+
}
180+
let nxt: Option<Box<ListNode>> = ptr.take().unwrap().next.take();
181+
*ptr = nxt;
182+
self.cnt -= 1i32;
183+
}
184+
}
185+
186+
/**
187+
* Your MyLinkedList object will be instantiated and called as such:
188+
* let obj = MyLinkedList::new();
189+
* let ret_1: i32 = obj.get(index);
190+
* obj.add_at_head(val);
191+
* obj.add_at_tail(val);
192+
* obj.add_at_index(index, val);
193+
* obj.delete_at_index(index);
194+
*/
195+
```

0 commit comments

Comments
 (0)
0