-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.合并两个有序链表.rs
91 lines (89 loc) · 1.91 KB
/
21.合并两个有序链表.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* @lc app=leetcode.cn id=21 lang=rust
*
* [21] 合并两个有序链表
*
* https://leetcode-cn.com/problems/merge-two-sorted-lists/description/
*
* algorithms
* Easy (66.74%)
* Likes: 2004
* Dislikes: 0
* Total Accepted: 753.9K
* Total Submissions: 1.1M
* Testcase Example: '[1,2,4]\n[1,3,4]'
*
* 将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
*
*
*
* 示例 1:
*
*
* 输入:l1 = [1,2,4], l2 = [1,3,4]
* 输出:[1,1,2,3,4,4]
*
*
* 示例 2:
*
*
* 输入:l1 = [], l2 = []
* 输出:[]
*
*
* 示例 3:
*
*
* 输入:l1 = [], l2 = [0]
* 输出:[0]
*
*
*
*
* 提示:
*
*
* 两个链表的节点数目范围是 [0, 50]
* -100
* l1 和 l2 均按 非递减顺序 排列
*
*
*/
// @lc code=start
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
match (l1, l2) {
(Some(node1), None) => Some(node1),
(None, Some(node2)) => Some(node2),
(Some(mut node1), Some(mut node2)) => {
if node1.val < node2.val {
let n = node1.next.take();
node1.next = Solution::merge_two_lists(n, Some(node2));
Some(node1)
} else {
let n = node2.next.take();
node2.next = Solution::merge_two_lists(n, Some(node1));
Some(node2)
}
}
_ => None,
}
}
}
// @lc code=end