Skip to content

Commit 1c1bff4

Browse files
committed
feat: add rust solution to lcci problem: No.02.02
No.02.02.Kth Node From End of List
1 parent 02a22c4 commit 1c1bff4

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

lcci/02.02.Kth Node From End of List/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,41 @@ func kthToLast(head *ListNode, k int) int {
150150
}
151151
```
152152

153+
### **Rust**
154+
155+
```rust
156+
// Definition for singly-linked list.
157+
// #[derive(PartialEq, Eq, Clone, Debug)]
158+
// pub struct ListNode {
159+
// pub val: i32,
160+
// pub next: Option<Box<ListNode>>
161+
// }
162+
//
163+
// impl ListNode {
164+
// #[inline]
165+
// fn new(val: i32) -> Self {
166+
// ListNode {
167+
// next: None,
168+
// val
169+
// }
170+
// }
171+
// }
172+
impl Solution {
173+
pub fn kth_to_last(head: Option<Box<ListNode>>, k: i32) -> i32 {
174+
let mut fast = &head;
175+
for _ in 0..k {
176+
fast = &fast.as_ref().unwrap().next;
177+
}
178+
let mut slow = &head;
179+
while let (Some(f), Some(s)) = (fast, slow) {
180+
fast = &f.next;
181+
slow = &s.next;
182+
}
183+
slow.as_ref().unwrap().val
184+
}
185+
}
186+
```
187+
153188
### **...**
154189

155190
```

lcci/02.02.Kth Node From End of List/README_EN.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ func kthToLast(head *ListNode, k int) int {
142142
}
143143
```
144144

145+
### **Rust**
146+
147+
```rust
148+
// Definition for singly-linked list.
149+
// #[derive(PartialEq, Eq, Clone, Debug)]
150+
// pub struct ListNode {
151+
// pub val: i32,
152+
// pub next: Option<Box<ListNode>>
153+
// }
154+
//
155+
// impl ListNode {
156+
// #[inline]
157+
// fn new(val: i32) -> Self {
158+
// ListNode {
159+
// next: None,
160+
// val
161+
// }
162+
// }
163+
// }
164+
impl Solution {
165+
pub fn kth_to_last(head: Option<Box<ListNode>>, k: i32) -> i32 {
166+
let mut fast = &head;
167+
for _ in 0..k {
168+
fast = &fast.as_ref().unwrap().next;
169+
}
170+
let mut slow = &head;
171+
while let (Some(f), Some(s)) = (fast, slow) {
172+
fast = &f.next;
173+
slow = &s.next;
174+
}
175+
slow.as_ref().unwrap().val
176+
}
177+
}
178+
```
179+
145180
### **...**
146181

147182
```
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Definition for singly-linked list.
2+
// #[derive(PartialEq, Eq, Clone, Debug)]
3+
// pub struct ListNode {
4+
// pub val: i32,
5+
// pub next: Option<Box<ListNode>>
6+
// }
7+
//
8+
// impl ListNode {
9+
// #[inline]
10+
// fn new(val: i32) -> Self {
11+
// ListNode {
12+
// next: None,
13+
// val
14+
// }
15+
// }
16+
// }
17+
impl Solution {
18+
pub fn kth_to_last(head: Option<Box<ListNode>>, k: i32) -> i32 {
19+
let mut fast = &head;
20+
for _ in 0..k {
21+
fast = &fast.as_ref().unwrap().next;
22+
}
23+
let mut slow = &head;
24+
while let (Some(f), Some(s)) = (fast, slow) {
25+
fast = &f.next;
26+
slow = &s.next;
27+
}
28+
slow.as_ref().unwrap().val
29+
}
30+
}

0 commit comments

Comments
 (0)