Skip to content

[DaleSeo] WEEK 04 solutions #1829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions merge-two-sorted-lists/DaleSeo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// TC: O(m + n)
// SC: O(1)
impl Solution {
pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {
let mut dummy = Box::new(ListNode::new(-1));
let mut node = &mut dummy;
let (mut l1, mut l2) = (list1, list2);
while l1.is_some() && l2.is_some() {
let val1 = l1.as_ref().unwrap().val;
let val2 = l2.as_ref().unwrap().val;
if val1 < val2 {
node.next = l1;
node = node.next.as_mut().unwrap();
l1 = node.next.take();
} else {
node.next = l2;
node = node.next.as_mut().unwrap();
l2 = node.next.take();
}
}
node.next = l1.or(l2);
dummy.next
}
}