-
Notifications
You must be signed in to change notification settings - Fork 24
/
lib.rs
64 lines (63 loc) · 1.29 KB
/
lib.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
/*
* @lc app=leetcode.cn id=1669 lang=rust
*
* [1669] 合并两个链表
*/
//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 }
}
}
struct Solution {}
// @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_in_between(
mut list1: Option<Box<ListNode>>,
a: i32,
b: i32,
list2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut head_clone = list1.clone();
let mut head = &mut list1;
let mut left = 1;
let mut right = 1;
while right != b + 2 {
head_clone = head_clone?.next;
right += 1;
}
while left != a {
head = &mut head.as_mut()?.next;
left += 1;
}
head.as_mut()?.next = list2;
while head.as_mut()?.next.is_some() {
head = &mut head.as_mut()?.next;
}
head.as_mut()?.next = head_clone;
list1
}
}
// @lc code=end