Skip to content

Commit 6edce70

Browse files
committed
feat: add more
1 parent a0ac931 commit 6edce70

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ members = [
88
"sliding_window",
99
"String",
1010
"greedy",
11+
"linked_list",
1112
"Tree",
1213
"trie",
1314
"stack",

linked_list/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "leetcode_linked_list"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
[lib]
8+
path = "lib.rs"
9+
doctest = false
10+
11+
[dependencies]
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// https://leetcode.com/problems/merge-two-sorted-lists
2+
//
3+
// You are given the heads of two sorted linked lists `list1` and `list2`.
4+
//
5+
// Merge the two lists in a one **sorted** list. The list should be made by splicing together the nodes of the first two lists.
6+
//
7+
// Return _the head of the merged linked list_.
8+
//
9+
// **Example 1:**
10+
//
11+
// ![](https://assets.leetcode.com/uploads/2020/10/03/merge_ex1.jpg)
12+
// ```
13+
// **Input:** list1 = [1,2,4], list2 = [1,3,4]
14+
// **Output:** [1,1,2,3,4,4]
15+
// ```
16+
//
17+
// **Example 2:**
18+
//
19+
// ```
20+
// **Input:** list1 = [], list2 = []
21+
// **Output:** []
22+
// ```
23+
//
24+
// **Example 3:**
25+
//
26+
// ```
27+
// **Input:** list1 = [], list2 = [0]
28+
// **Output:** [0]
29+
// ```
30+
//
31+
// **Constraints:**
32+
//
33+
// * The number of nodes in both lists is in the range `[0, 50]`.
34+
// * `-100 <= Node.val <= 100`
35+
// * Both `list1` and `list2` are sorted in **non-decreasing** order.
36+
use crate::list_node::ListNode;
37+
38+
pub fn merge_two_lists(
39+
list1: Option<Box<ListNode>>,
40+
list2: Option<Box<ListNode>>,
41+
) -> Option<Box<ListNode>> {
42+
let mut list1 = list1.as_ref();
43+
let mut list2 = list2.as_ref();
44+
let mut start = Box::new(ListNode::new(0));
45+
let mut end = &mut start;
46+
while let (Some(n1), Some(n2)) = (list1, list2) {
47+
if n1.val < n2.val {
48+
end.next = list1.cloned();
49+
list1 = n1.next.as_ref();
50+
} else {
51+
end.next = list2.cloned();
52+
list2 = n2.next.as_ref();
53+
}
54+
end = end.next.as_mut().unwrap();
55+
}
56+
if list1.is_some() {
57+
end.next = list1.cloned();
58+
} else if list2.is_some() {
59+
end.next = list2.cloned();
60+
}
61+
return start.next;
62+
}
63+
64+
#[test]
65+
pub fn t1() {
66+
assert_eq!(
67+
merge_two_lists(
68+
Some(Box::new(ListNode {
69+
val: 1,
70+
next: Some(Box::new(ListNode {
71+
val: 2,
72+
next: Some(Box::new(ListNode { val: 4, next: None })),
73+
})),
74+
})),
75+
Some(Box::new(ListNode {
76+
val: 1,
77+
next: Some(Box::new(ListNode {
78+
val: 3,
79+
next: Some(Box::new(ListNode { val: 4, next: None })),
80+
})),
81+
})),
82+
),
83+
Some(Box::new(ListNode {
84+
val: 1,
85+
next: Some(Box::new(ListNode {
86+
val: 1,
87+
next: Some(Box::new(ListNode {
88+
val: 2,
89+
next: Some(Box::new(ListNode {
90+
val: 3,
91+
next: Some(Box::new(ListNode {
92+
val: 4,
93+
next: Some(Box::new(ListNode { val: 4, next: None })),
94+
})),
95+
})),
96+
})),
97+
})),
98+
}))
99+
);
100+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// https://leetcode.com/problems/reverse-linked-list
2+
//
3+
// Given the `head` of a singly linked list, reverse the list, and return _the reversed list_.
4+
//
5+
// **Example 1:**
6+
//
7+
// ![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
8+
// ```
9+
// **Input:** head = [1,2,3,4,5]
10+
// **Output:** [5,4,3,2,1]
11+
// ```
12+
//
13+
// **Example 2:**
14+
//
15+
// ![](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)
16+
// ```
17+
// **Input:** head = [1,2]
18+
// **Output:** [2,1]
19+
// ```
20+
//
21+
// **Example 3:**
22+
//
23+
// ```
24+
// **Input:** head = []
25+
// **Output:** []
26+
// ```
27+
//
28+
// **Constraints:**
29+
//
30+
// * The number of nodes in the list is the range `[0, 5000]`.
31+
// * `-5000 <= Node.val <= 5000`
32+
//
33+
// **Follow up:** A linked list can be reversed either iteratively or recursively. Could you implement both?
34+
35+
use crate::list_node::ListNode;
36+
37+
pub fn reverse_list(head: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
38+
let (mut head, mut prev) = (head, None);
39+
while let Some(mut node) = head {
40+
head = node.next.take();
41+
node.next = prev;
42+
prev = Some(node);
43+
}
44+
return prev;
45+
}
46+
47+
#[test]
48+
pub fn t1() {
49+
assert_eq!(
50+
reverse_list(Some(Box::new(ListNode {
51+
val: 1,
52+
next: Some(Box::new(ListNode {
53+
val: 2,
54+
next: Some(Box::new(ListNode {
55+
val: 3,
56+
next: Some(Box::new(ListNode {
57+
val: 4,
58+
next: Some(Box::new(ListNode { val: 5, next: None })),
59+
})),
60+
})),
61+
})),
62+
}))),
63+
Some(Box::new(ListNode {
64+
val: 5,
65+
next: Some(Box::new(ListNode {
66+
val: 4,
67+
next: Some(Box::new(ListNode {
68+
val: 3,
69+
next: Some(Box::new(ListNode {
70+
val: 2,
71+
next: Some(Box::new(ListNode { val: 1, next: None })),
72+
})),
73+
})),
74+
})),
75+
}))
76+
);
77+
}

linked_list/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#![allow(dead_code)]
2+
3+
mod _0021_merge_two_sorted_lists;
4+
mod _0206_reverse_linked_list;
5+
mod list_node;

linked_list/list_node.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
pub fn new(val: i32) -> Self {
11+
ListNode { next: None, val }
12+
}
13+
}

0 commit comments

Comments
 (0)