Skip to content

Commit 5881e6a

Browse files
committed
feat: update rust solution to lc problem: No.0707
No.0707.Design Linked List
1 parent 3bc25f6 commit 5881e6a

File tree

3 files changed

+88
-121
lines changed

3 files changed

+88
-121
lines changed

solution/0700-0799/0707.Design Linked List/README.md

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -835,14 +835,9 @@ class MyLinkedList {
835835
### **Rust**
836836

837837
```rust
838-
struct Node {
839-
val: i32,
840-
next: Option<Box<Node>>,
841-
}
842-
843838
#[derive(Default)]
844839
struct MyLinkedList {
845-
head: Option<Box<Node>>,
840+
head: Option<Box<ListNode>>,
846841
}
847842

848843
/**
@@ -854,80 +849,74 @@ impl MyLinkedList {
854849
Default::default()
855850
}
856851

857-
fn get(&self, index: i32) -> i32 {
858-
let mut cur = match self.head {
859-
None => return -1,
860-
Some(ref n) => n,
861-
};
862-
let mut idx_cur = 0;
863-
while idx_cur < index {
852+
fn get(&self, mut index: i32) -> i32 {
853+
if self.head.is_none() {
854+
return -1;
855+
}
856+
let mut cur = self.head.as_ref().unwrap();
857+
while index > 0 {
864858
match cur.next {
865859
None => return -1,
866860
Some(ref next) => {
867861
cur = next;
868-
idx_cur += 1;
862+
index -= 1;
869863
}
870864
}
871865
}
872866
cur.val
873867
}
874868

875869
fn add_at_head(&mut self, val: i32) {
876-
self.head = Some(Box::new(Node {
870+
self.head = Some(Box::new(ListNode {
877871
val,
878872
next: self.head.take(),
879873
}));
880874
}
881875

882876
fn add_at_tail(&mut self, val: i32) {
883-
let new_node = Some(Box::new(Node { val, next: None }));
884-
let mut cur = match self.head {
885-
Some(ref mut n) => n,
886-
None => {
887-
self.head = new_node;
888-
return;
889-
}
890-
};
877+
let new_node = Some(Box::new(ListNode { val, next: None }));
878+
if self.head.is_none() {
879+
self.head = new_node;
880+
return;
881+
}
882+
let mut cur = self.head.as_mut().unwrap();
891883
while let Some(ref mut next) = cur.next {
892884
cur = next;
893885
}
894886
cur.next = new_node;
895887
}
896888

897-
fn add_at_index(&mut self, index: i32, val: i32) {
898-
let mut dummy = Box::new(Node {
889+
fn add_at_index(&mut self, mut index: i32, val: i32) {
890+
let mut dummy = Box::new(ListNode {
899891
val: 0,
900-
next: self.head.take()
892+
next: self.head.take(),
901893
});
902-
let mut idx = 0;
903894
let mut cur = &mut dummy;
904-
while idx < index {
905-
if let Some(ref mut next) = cur.next {
906-
cur = next;
907-
} else {
908-
return
895+
while index > 0 {
896+
if cur.next.is_none() {
897+
return;
909898
}
910-
idx += 1;
899+
cur = cur.next.as_mut().unwrap();
900+
index -= 1;
911901
}
912-
cur.next = Some(Box::new(Node {
902+
cur.next = Some(Box::new(ListNode {
913903
val,
914-
next: cur.next.take()
904+
next: cur.next.take(),
915905
}));
916906
self.head = dummy.next;
917907
}
918908

919-
fn delete_at_index(&mut self, index: i32) {
920-
let mut dummy = Box::new(Node {
909+
fn delete_at_index(&mut self, mut index: i32) {
910+
let mut dummy = Box::new(ListNode {
921911
val: 0,
922912
next: self.head.take(),
923913
});
924-
let mut idx = 0;
925914
let mut cur = &mut dummy;
926-
while idx < index {
915+
while index > 0 {
927916
if let Some(ref mut next) = cur.next {
928917
cur = next;
929918
}
930-
idx += 1;
919+
index -= 1;
931920
}
932921
cur.next = cur.next.take().and_then(|n| n.next);
933922
self.head = dummy.next;

solution/0700-0799/0707.Design Linked List/README_EN.md

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -804,14 +804,9 @@ class MyLinkedList {
804804
### **Rust**
805805

806806
```rust
807-
struct Node {
808-
val: i32,
809-
next: Option<Box<Node>>,
810-
}
811-
812807
#[derive(Default)]
813808
struct MyLinkedList {
814-
head: Option<Box<Node>>,
809+
head: Option<Box<ListNode>>,
815810
}
816811

817812
/**
@@ -823,80 +818,74 @@ impl MyLinkedList {
823818
Default::default()
824819
}
825820

826-
fn get(&self, index: i32) -> i32 {
827-
let mut cur = match self.head {
828-
None => return -1,
829-
Some(ref n) => n,
830-
};
831-
let mut idx_cur = 0;
832-
while idx_cur < index {
821+
fn get(&self, mut index: i32) -> i32 {
822+
if self.head.is_none() {
823+
return -1;
824+
}
825+
let mut cur = self.head.as_ref().unwrap();
826+
while index > 0 {
833827
match cur.next {
834828
None => return -1,
835829
Some(ref next) => {
836830
cur = next;
837-
idx_cur += 1;
831+
index -= 1;
838832
}
839833
}
840834
}
841835
cur.val
842836
}
843837

844838
fn add_at_head(&mut self, val: i32) {
845-
self.head = Some(Box::new(Node {
839+
self.head = Some(Box::new(ListNode {
846840
val,
847841
next: self.head.take(),
848842
}));
849843
}
850844

851845
fn add_at_tail(&mut self, val: i32) {
852-
let new_node = Some(Box::new(Node { val, next: None }));
853-
let mut cur = match self.head {
854-
Some(ref mut n) => n,
855-
None => {
856-
self.head = new_node;
857-
return;
858-
}
859-
};
846+
let new_node = Some(Box::new(ListNode { val, next: None }));
847+
if self.head.is_none() {
848+
self.head = new_node;
849+
return;
850+
}
851+
let mut cur = self.head.as_mut().unwrap();
860852
while let Some(ref mut next) = cur.next {
861853
cur = next;
862854
}
863855
cur.next = new_node;
864856
}
865857

866-
fn add_at_index(&mut self, index: i32, val: i32) {
867-
let mut dummy = Box::new(Node {
858+
fn add_at_index(&mut self, mut index: i32, val: i32) {
859+
let mut dummy = Box::new(ListNode {
868860
val: 0,
869-
next: self.head.take()
861+
next: self.head.take(),
870862
});
871-
let mut idx = 0;
872863
let mut cur = &mut dummy;
873-
while idx < index {
874-
if let Some(ref mut next) = cur.next {
875-
cur = next;
876-
} else {
877-
return
864+
while index > 0 {
865+
if cur.next.is_none() {
866+
return;
878867
}
879-
idx += 1;
868+
cur = cur.next.as_mut().unwrap();
869+
index -= 1;
880870
}
881-
cur.next = Some(Box::new(Node {
871+
cur.next = Some(Box::new(ListNode {
882872
val,
883-
next: cur.next.take()
873+
next: cur.next.take(),
884874
}));
885875
self.head = dummy.next;
886876
}
887877

888-
fn delete_at_index(&mut self, index: i32) {
889-
let mut dummy = Box::new(Node {
878+
fn delete_at_index(&mut self, mut index: i32) {
879+
let mut dummy = Box::new(ListNode {
890880
val: 0,
891881
next: self.head.take(),
892882
});
893-
let mut idx = 0;
894883
let mut cur = &mut dummy;
895-
while idx < index {
884+
while index > 0 {
896885
if let Some(ref mut next) = cur.next {
897886
cur = next;
898887
}
899-
idx += 1;
888+
index -= 1;
900889
}
901890
cur.next = cur.next.take().and_then(|n| n.next);
902891
self.head = dummy.next;

solution/0700-0799/0707.Design Linked List/Solution.rs

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
struct Node {
2-
val: i32,
3-
next: Option<Box<Node>>,
4-
}
5-
61
#[derive(Default)]
72
struct MyLinkedList {
8-
head: Option<Box<Node>>,
3+
head: Option<Box<ListNode>>,
94
}
105

116
/**
@@ -17,80 +12,74 @@ impl MyLinkedList {
1712
Default::default()
1813
}
1914

20-
fn get(&self, index: i32) -> i32 {
21-
let mut cur = match self.head {
22-
None => return -1,
23-
Some(ref n) => n,
24-
};
25-
let mut idx_cur = 0;
26-
while idx_cur < index {
15+
fn get(&self, mut index: i32) -> i32 {
16+
if self.head.is_none() {
17+
return -1;
18+
}
19+
let mut cur = self.head.as_ref().unwrap();
20+
while index > 0 {
2721
match cur.next {
2822
None => return -1,
2923
Some(ref next) => {
3024
cur = next;
31-
idx_cur += 1;
25+
index -= 1;
3226
}
3327
}
3428
}
3529
cur.val
3630
}
3731

3832
fn add_at_head(&mut self, val: i32) {
39-
self.head = Some(Box::new(Node {
33+
self.head = Some(Box::new(ListNode {
4034
val,
4135
next: self.head.take(),
4236
}));
4337
}
4438

4539
fn add_at_tail(&mut self, val: i32) {
46-
let new_node = Some(Box::new(Node { val, next: None }));
47-
let mut cur = match self.head {
48-
Some(ref mut n) => n,
49-
None => {
50-
self.head = new_node;
51-
return;
52-
}
53-
};
40+
let new_node = Some(Box::new(ListNode { val, next: None }));
41+
if self.head.is_none() {
42+
self.head = new_node;
43+
return;
44+
}
45+
let mut cur = self.head.as_mut().unwrap();
5446
while let Some(ref mut next) = cur.next {
5547
cur = next;
5648
}
5749
cur.next = new_node;
5850
}
5951

60-
fn add_at_index(&mut self, index: i32, val: i32) {
61-
let mut dummy = Box::new(Node {
52+
fn add_at_index(&mut self, mut index: i32, val: i32) {
53+
let mut dummy = Box::new(ListNode {
6254
val: 0,
63-
next: self.head.take()
55+
next: self.head.take(),
6456
});
65-
let mut idx = 0;
6657
let mut cur = &mut dummy;
67-
while idx < index {
68-
if let Some(ref mut next) = cur.next {
69-
cur = next;
70-
} else {
71-
return
58+
while index > 0 {
59+
if cur.next.is_none() {
60+
return;
7261
}
73-
idx += 1;
62+
cur = cur.next.as_mut().unwrap();
63+
index -= 1;
7464
}
75-
cur.next = Some(Box::new(Node {
76-
val,
77-
next: cur.next.take()
65+
cur.next = Some(Box::new(ListNode {
66+
val,
67+
next: cur.next.take(),
7868
}));
7969
self.head = dummy.next;
8070
}
8171

82-
fn delete_at_index(&mut self, index: i32) {
83-
let mut dummy = Box::new(Node {
72+
fn delete_at_index(&mut self, mut index: i32) {
73+
let mut dummy = Box::new(ListNode {
8474
val: 0,
8575
next: self.head.take(),
8676
});
87-
let mut idx = 0;
8877
let mut cur = &mut dummy;
89-
while idx < index {
78+
while index > 0 {
9079
if let Some(ref mut next) = cur.next {
9180
cur = next;
9281
}
93-
idx += 1;
82+
index -= 1;
9483
}
9584
cur.next = cur.next.take().and_then(|n| n.next);
9685
self.head = dummy.next;

0 commit comments

Comments
 (0)