Kotlin | Rust
\nhttps://youtu.be/0NU6p7K7INY\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/544\n\n#### Problem TLDR\n\nReplace a segment in a LinkedLis
![2024-03-20_09-48.jpg](https://assets.leetcode.com/users/images/ff2150a7-c8fe-4eed-9636-d94366c925fb_1710917336.6740346.jpeg)\nhttps://youtu.be/0NU6p7K7INY\n#### Join me on Telegram\n\nhttps://t.me/leetcode_daily_unstoppable/544\n\n#### Problem TLDR\n\nReplace a segment in a LinkedList #medium\n\n#### Intuition\n\nJust careful pointers iteration.\n\n#### Approach\n\n* use dummy to handle the first node removal\n* better to write a separate cycles\n* Rust is hard\n\n#### Complexity\n\n- Time complexity:\n$$O(n)$$\n\n- Space complexity:\n$$O(1)$$\n\n#### Code\n\n```kotlin []\n\n  fun mergeInBetween(list1: ListNode?, a: Int, b: Int, list2: ListNode?) = \n    ListNode(0).run {\n      next = list1\n      var curr: ListNode? = this\n      for (i in 1..a) curr = curr?.next\n      var after = curr?.next\n      for (i in a..b) after = after?.next\n      curr?.next = list2\n      while (curr?.next != null) curr = curr?.next\n      curr?.next = after\n      next\n    }\n\n```\n```rust []\n\n  pub fn merge_in_between(list1: Option<Box<ListNode>>, a: i32, b: i32, list2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n    let mut dummy = Box::new(ListNode::new(0));\n    dummy.next = list1;\n    let mut curr = &mut dummy;\n    for _ in 0..a { curr = curr.next.as_mut().unwrap() }\n    let mut after = &mut curr.next;\n    for _ in a..=b { after = &mut after.as_mut().unwrap().next }\n    let after_b = after.take(); // Detach the rest of the list after `b`, this will allow the next line for the borrow checker\n    curr.next = list2;\n    while let Some(ref mut next) = curr.next { curr = next; }\n    curr.next = after_b;\n    dummy.next\n  }\n\n```
['Rust', 'Kotlin']