Re: [闲聊] 每日leetcode

楼主: yam276 ('_')   2024-03-20 16:47:56
※ 引述《sustainer123 (caster )》之铭言:
: https://leetcode.com/problems/merge-in-between-linked-lists
: 1669. Merge In Between Linked Lists
: 给你两个链表(list1 and list2)与两个数字(a and b)
: 你需要移除list1中a到b的节点并替换为list2
借用跟所有权太难搞了
概念都懂但写出来过不了Compiler
只好去看别人的Code:
impl Solution {
pub fn merge_in_between(list1: Option<Box<ListNode>>, a: i32, b: i32,
list2: Option<Box<ListNode>>)
-> Option<Box<ListNode>>
{
let mut dummy = Box::new(ListNode::new(0));
dummy.next = list1;
let mut curr = &mut dummy;
for _ in 0..a {
curr = curr.next.as_mut().unwrap()
}
let mut after = &mut curr.next;
for _ in a..=b {
after = &mut after.as_mut().unwrap().next
}
let after_b = after.take();
// Detach the rest of the list after `b`,
// this will allow the next line for the borrow checker
curr.next = list2;
while let Some(ref mut next) = curr.next {
curr = next;
}
curr.next = after_b;
dummy.next
}
}
作者: wwndbk (黑人问号)   2024-03-20 16:48:00
qs
作者: sustainer123 (caster)   2024-03-20 18:06:00
太苦了

Links booklink

Contact Us: admin [ a t ] ucptt.com