|
| 1 | +#[derive(PartialEq, Eq, Debug, Clone)] |
| 2 | +pub struct ListNode { |
| 3 | + pub val: i32, |
| 4 | + pub next: Option<Box<ListNode>>, |
| 5 | +} |
| 6 | + |
| 7 | +impl ListNode { |
| 8 | + #[inline] |
| 9 | + pub fn new(val: i32) -> Self { |
| 10 | + ListNode { next: None, val } |
| 11 | + } |
| 12 | + |
| 13 | + // For testing only |
| 14 | + pub fn add_node(&mut self, node: ListNode) -> Self { |
| 15 | + match self.next { |
| 16 | + Some(ref mut n) => { n.add_node(node); } |
| 17 | + None => self.next = Some(Box::new(node)), |
| 18 | + }; |
| 19 | + self.clone() |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl Iterator for ListNode { |
| 24 | + type Item = Box<ListNode>; |
| 25 | + |
| 26 | + fn next(&mut self) -> Option<Self::Item> { |
| 27 | + self.next.to_owned() |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +struct Solution {} |
| 32 | + |
| 33 | +impl Solution { |
| 34 | + pub fn merge_two_lists(list1: Option<Box<ListNode>>, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { |
| 35 | + let Some(mut list1_head) = list1 else { |
| 36 | + return list2 |
| 37 | + }; |
| 38 | + |
| 39 | + let Some(mut list2_head) = list2 else { |
| 40 | + return Some(list1_head) |
| 41 | + }; |
| 42 | + |
| 43 | + if list1_head.val < list2_head.val { |
| 44 | + list1_head.next = Solution::merge_two_lists(list1_head.next, Some(list2_head)); |
| 45 | + return Some(list1_head); |
| 46 | + } else { |
| 47 | + list2_head.next = Solution::merge_two_lists(Some(list1_head), list2_head.next); |
| 48 | + return Some(list2_head) |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[cfg(test)] |
| 54 | +mod tests { |
| 55 | + use super::*; |
| 56 | + |
| 57 | + #[test] |
| 58 | + fn case_1() { |
| 59 | + let head_of_list_1 = ListNode::new(1) |
| 60 | + .add_node(ListNode::new(2)) |
| 61 | + .add_node(ListNode::new(4)); |
| 62 | + |
| 63 | + println!("The first list is: {head_of_list_1:#?}"); |
| 64 | + |
| 65 | + let head_of_list_2 = ListNode::new(1) |
| 66 | + .add_node(ListNode::new(3)) |
| 67 | + .add_node(ListNode::new(4)); |
| 68 | + |
| 69 | + println!("The second list is {head_of_list_2:#?}"); |
| 70 | + |
| 71 | + let merged_list = Solution::merge_two_lists(Some(Box::new(head_of_list_1)), |
| 72 | + Some(Box::new(head_of_list_2))); |
| 73 | + |
| 74 | + let correct_list = ListNode::new(1) |
| 75 | + .add_node(ListNode::new(1)) |
| 76 | + .add_node(ListNode::new(2)) |
| 77 | + .add_node(ListNode::new(3)) |
| 78 | + .add_node(ListNode::new(4)) |
| 79 | + .add_node(ListNode::new(4)); |
| 80 | + |
| 81 | + assert_eq!(merged_list, Some(Box::new(correct_list))); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn case_2() { |
| 86 | + assert_eq!(Solution::merge_two_lists(None, None), None); |
| 87 | + } |
| 88 | + |
| 89 | + #[test] |
| 90 | + fn case_3() { |
| 91 | + assert_eq!(Solution::merge_two_lists(None, Some(Box::new(ListNode::new(0)))), |
| 92 | + Some(Box::new(ListNode::new(0)))); |
| 93 | + } |
| 94 | + |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn case_4() { |
| 98 | + let head_of_list_1 = ListNode::new(1) |
| 99 | + .add_node(ListNode::new(2)) |
| 100 | + .add_node(ListNode::new(3)); |
| 101 | + |
| 102 | + println!("The first list is: {head_of_list_1:#?}"); |
| 103 | + |
| 104 | + let head_of_list_2 = ListNode::new(4) |
| 105 | + .add_node(ListNode::new(5)) |
| 106 | + .add_node(ListNode::new(6)); |
| 107 | + |
| 108 | + println!("The second list is {head_of_list_2:#?}"); |
| 109 | + |
| 110 | + let merged_list = Solution::merge_two_lists(Some(Box::new(head_of_list_1)), |
| 111 | + Some(Box::new(head_of_list_2))); |
| 112 | + |
| 113 | + let correct_list = ListNode::new(1) |
| 114 | + .add_node(ListNode::new(2)) |
| 115 | + .add_node(ListNode::new(3)) |
| 116 | + .add_node(ListNode::new(4)) |
| 117 | + .add_node(ListNode::new(5)) |
| 118 | + .add_node(ListNode::new(6)); |
| 119 | + |
| 120 | + assert_eq!(merged_list, Some(Box::new(correct_list))); |
| 121 | + } |
| 122 | +} |
0 commit comments