Simple as pie || Effort less :)
Intuition\nTo solve this question we need to locate the nodes at positions a-1 and b+1 in list1,(Because as we know inorder to connect node next to other node w
# Intuition\nTo solve this question we need to locate the nodes at positions a-1 and b+1 in list1,(Because as we know inorder to connect node next to other node we need to get the previous node so we acquire the a-1 node,then we have to remove node from a to b so we get the b+1th node and connect to the existing LL) and then connect these nodes to the head and tail of list2, respectively.\n\n# Approach\n1.Traverse list1 to find the nodes at positions a-1 and b+1.\n2.Connect the node at position a-1 to the head of list2.\n3.Traverse list2 to find its last node.\n4.Connect the last node of list2 to the node at position b+1 in list1.\n5.Return list1 after merging list2.\n\n# Complexity\n- Time complexity:O(n)\n\n- Space complexity:O(1)\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    public ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {\n        \n        int i=0;\n        ListNode an=null,bn=null,temp=list1;\n        while(temp!=null)\n        {\n            if(i==a-1)\n            {\n                an=temp;\n            }\n            else if(i==b+1)\n            {\n                bn=temp;\n            }\n            temp=temp.next;\n            i++;\n        }\n        //connect a-1 node to the list 2 head\n        an.next=list2;\n\n        //traversing through the list 2 LL to find end of list2\n\n        ListNode temp2=list2;\n        while(temp2.next!=null)\n        {\n            temp2=temp2.next;\n        }\n\n        //connecting end of list2 to b+1 node of list\n        temp2.next=bn;\n\n        return list1;\n\n    }\n}\n```
['Java']