Beats 50% 🔥|| Smart Solution Easy 😎|| Notes++ || Java,C++,Python
IntuitionThis problem involves modifying a linked list by removing a portion from indices a to b and replacing it with another linked list. Let's break down the
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
This problem involves modifying a linked list by removing a portion from indices a to b and replacing it with another linked list. Let's break down the intuition and approach step by step.

# Approach
<!-- Describe your approach to solving the problem. -->
![image.png](https://assets.leetcode.com/users/images/aabeb87b-1a1a-471f-bef7-9c6937105048_1741693075.625773.png)

# NOTES 
![1.jpg](https://assets.leetcode.com/users/images/1a7c5a5e-20f7-437c-a483-e31bdc7a1383_1741693092.6897898.jpeg)
![2.jpg](https://assets.leetcode.com/users/images/d85221e3-4958-4b50-9d9d-9e6caf2eab23_1741693097.2821124.jpeg)

# Complexity
![image.png](https://assets.leetcode.com/users/images/b7304216-a11a-4438-bd47-0afd1a417ac2_1741693032.535699.png)


# Code
```java []
class Solution {
    public ListNode mergeInBetween(ListNode l1, int a, int b, ListNode l2) {
        ListNode left=null,right=l1;
        for(int i=0;i<=b;i++){
            if(i==a-1) left=right;
            right = right.next;
        }
        left.next = l2;
        ListNode temp = l2;
        while(temp!=null && temp.next!=null){
            temp = temp.next;
        }
        temp.next = right;
        return l1;
    }
}
```
```c++ []
#include <iostream>
using namespace std;

// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

class Solution {
public:
    ListNode* mergeInBetween(ListNode* l1, int a, int b, ListNode* l2) {
        ListNode* left = nullptr;
        ListNode* right = l1;
        
        // Traverse the list to find 'left' and 'right'
        for (int i = 0; i <= b; i++) {
            if (i == a - 1) left = right;
            right = right->next;
        }
        
        // Connect left part to l2
        left->next = l2;
        
        // Traverse l2 to find its last node
        ListNode* temp = l2;
        while (temp->next != nullptr) {
            temp = temp->next;
        }
        
        // Connect last node of l2 to right
        temp->next = right;
        
        return l1;
    }
};
```
```python []
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def mergeInBetween(self, l1: ListNode, a: int, b: int, l2: ListNode) -> ListNode:
        left, right = None, l1
        
        # Find the left (a-1) and right (b+1) nodes
        for i in range(b + 1):
            if i == a - 1:
                left = right
            right = right.next
        
        # Connect left to l2
        left.next = l2
        
        # Traverse l2 to find the last node
        temp = l2
        while temp.next:
            temp = temp.next
        
        # Connect last node of l2 to right
        temp.next = right
        
        return l1
```
![ok.png](https://lh6.googleusercontent.com/proxy/c1LeLv7686Z3O0EwMYz1lmGtbXDKOiBX-BuRnwKeGTNuZe0BvX8zRUkiGoFz8uYyPzx4HakbmPxP_Vtd0EUIAPOoej-NiUQub0ONve7eniMitSoN-mHyAeT499TCJF8KA1cTRUiEqyg4MoHYvmeuJGxj-ssy)
['Linked List', 'Python', 'C++', 'Java', 'JavaScript']