Easy to follow straight forward solution ✅ || O(n) time and O(1) space ✅ || clean code ✅
Intuition\n Describe your first thoughts on how to solve this problem. \n- We only require the addresses of nodes located at indices a and b. \n- We can navigat
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- We only require the addresses of nodes located at indices `a` and `b`. \n- We can navigate through the list until we reach the node at position `a`, then connect it with `list2`. \n- After that, we can reconnect it back to its original position using the address of the node at position `b`.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. Start by declaring two variables: `addressOfa` and `addressOfb`.\n2. Traverse through `list1` to find the addresses of nodes at positions `a` and `b`.\n3. Begin traversing `list1` using a temporary variable `temp`. If you encounter a node whose address matches `addressOfa`, change its next pointer to point to `list2`.\n4. Traverse through `list2` from this point until the end.\n5. Connect the end of `list2` with the node at address `addressOfb`.\n6. Finally, return `list1`.\n\n# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * Definition for singly-linked list.\n * public class ListNode {\n *     int val;\n *     ListNode next;\n *     ListNode() {}\n *     ListNode(int val) { this.val = val; }\n *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }\n * }\n */\nclass Solution \n{\n    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) \n    {\n        ListNode addressOfa = null;        \n        ListNode addressOfb = null;\n        ListNode temp = list1;\n        int ind = 0;\n        // finding addresses of nodes at indices a and b\n        while(temp != null)\n        {\n            if(ind == a)\n            {\n                addressOfa = temp;\n            }\n            if(ind == b)\n            {\n                addressOfb = temp;\n            }\n            temp = temp.next;\n            ind ++;\n        }\n        // traverse till addressOfa and change address of that node to list 2\n        temp = list1;\n        while(true)\n        {\n            if(temp.next == addressOfa)\n            {\n                temp.next = list2;\n                break;\n            }\n            temp = temp.next;\n        }\n        // go to end of list 2\n        while(temp.next != null)\n        {\n            temp = temp.next;\n        }\n        // connect it back to list 1 with help of addressOfb\n        temp.next = addressOfb.next;\n        return list1;\n    }\n}\n```
['Linked List', 'Java']