Skip to content

Commit cba395a

Browse files
Merge pull request #351 from caitlingao/feat-rust-07-linkedlist
feat(geektime_algo): add 07 linkedlists algo
2 parents dc206af + cdced5a commit cba395a

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use super::util::linked_list::{ListNode, to_list};
2+
3+
pub fn has_cycle(head: Option<Box<ListNode>>) -> bool {
4+
let mut fast_p = &head;
5+
let mut slow_p = &head;
6+
7+
while fast_p.is_some() && fast_p.as_ref().unwrap().next.is_some() {
8+
slow_p = &slow_p.as_ref().unwrap().next;
9+
fast_p = &fast_p.as_ref().unwrap().next.as_ref().unwrap().next;
10+
11+
if slow_p == fast_p { return true; }
12+
}
13+
false
14+
}
15+
16+
fn main() {
17+
println!("{:?}", has_cycle(to_list(vec![1, 2, 3, 4, 5])));
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use super::util::linked_list::{ListNode, to_list};
2+
3+
pub fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
4+
match (l1, l2) {
5+
(Some(node1), None) => Some(node1),
6+
(None, Some(node2)) => Some(node2),
7+
(Some(mut node1), Some(mut node2)) => {
8+
if node1.val < node2.val {
9+
let n = node1.next.take();
10+
node1.next = Solution::merge_two_lists(n, Some(node2));
11+
Some(node1)
12+
} else {
13+
let n = node2.next.take();
14+
node2.next = Solution::merge_two_lists(Some(node1), n);
15+
Some(node2)
16+
}
17+
},
18+
_ => None,
19+
}
20+
}
21+
22+
fn main() {
23+
println!("{:?}", merge_two_lists(to_list(vec![1, 3, 4]), to_list(vec![1, 2, 4])));
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use super::util::linked_list::{ListNode, to_list};
2+
3+
pub fn middle_node(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
4+
let mut fast_p = &head;
5+
let mut slow_p = &head;
6+
7+
while fast_p.is_some() && fast_p.as_ref().unwrap().next.is_some() {
8+
slow_p = &slow_p.as_ref().unwrap().next;
9+
fast_p = &fast_p.as_ref().unwrap().next.as_ref().unwrap().next;
10+
}
11+
slow_p.clone()
12+
}
13+
14+
fn main() {
15+
println!("{:?}", middle_node(to_list(vec![1, 3, 4])));
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::util::linked_list::{ListNode, to_list};
2+
3+
pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> {
4+
let mut dummy = Some(Box::new(ListNode { val: 0, next: head }));
5+
let mut cur = &mut dummy;
6+
let mut length = 0;
7+
8+
while let Some(_node) = cur.as_mut() {
9+
cur = &mut cur.as_mut().unwrap().next;
10+
if let Some(_inner_node) = cur { length += 1; }
11+
}
12+
13+
let mut new_cur = dummy.as_mut();
14+
let idx = length - n;
15+
16+
for _ in 0..idx {
17+
new_cur = new_cur.unwrap().next.as_mut();
18+
}
19+
20+
let next = new_cur.as_mut().unwrap().next.as_mut().unwrap().next.take();
21+
new_cur.as_mut().unwrap().next = next;
22+
23+
dummy.unwrap().next
24+
}
25+
26+
fn main() {
27+
println!("{:?}", remove_nth_from_end(to_list(vec![1, 3, 4])));
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use super::util::linked_list::{ListNode, to_list};
2+
3+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
4+
let mut prev = None;
5+
let mut curr = head;
6+
7+
while let Some(mut boxed_node) = curr.take() {
8+
let next = boxed_node.next.take();
9+
boxed_node.next = prev.take();
10+
11+
prev = Some(boxed_node);
12+
curr = next;
13+
}
14+
15+
prev
16+
}
17+
18+
fn main() {
19+
println!("{:?}", reverse_list(to_list(vec![1, 2, 3, 4, 5])));
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
18+
pub fn to_list(vec: Vec<i32>) -> Option<Box<ListNode>> {
19+
let mut current = None;
20+
for &v in vec.iter().rev() {
21+
let mut node = ListNode::new(v);
22+
node.next = current;
23+
current = Some(Box::new(node));
24+
}
25+
current
26+
}
27+
28+
#[macro_export]
29+
macro_rules! linked {
30+
($($e:expr),*) => {to_list(vec![$($e.to_owned()), *])};
31+
($($e:expr,)*) => {to_list(vec![$($e.to_owned()), *])};
32+
}

0 commit comments

Comments
 (0)