Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (史萊哲林的優等生)時間1年前 (2024/03/20 16:47), 編輯推噓2(200)
留言2則, 2人參與, 1年前最新討論串67/1548 (看更多)
※ 引述《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 } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.251.123.162 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1710924478.A.555.html

03/20 16:48, 1年前 , 1F
qs
03/20 16:48, 1F

03/20 18:06, 1年前 , 2F
太苦了
03/20 18:06, 2F
文章代碼(AID): #1b-gA-LL (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1b-gA-LL (Marginalman)