Merge In Between Linked Lists

You are given two linked lists, list1 and list2, of sizes n and m. Remove the nodes of list1 from the a-th node to the b-th node (0-indexed), and put list2 in their place. Return the head of the resulting list.

30:00

Merge In Between Linked Lists
medium
Free written solution
Topics
Companies

You are given two linked lists, list1 and list2, of sizes n and m. Remove the nodes of list1 from the a-th node to the b-th node (0-indexed), and put list2 in their place. Return the head of the resulting list.

Example 1:
Input: {"list1":[10,1,13,6,9,5],"a":3,"b":4,"list2":[1000000,1000001,1000002]}
Output: [10,1,13,1000000,1000001,1000002,5]
Constraints:
  • 3≤list1.length≤1043 \leq \text{list1.length} \leq 10^4

  • 1≤a≤b<list1.length−11 \leq a \leq b < \text{list1.length} - 1

  • 1≤list2.length≤1041 \leq \text{list2.length} \leq 10^4

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"list1":[10,1,13,6,9,5],"a":3,"b":4,"list2":[1000000,1000001,1000002]}

Nodes 3 to 4 of list1 must be cut out and list2 put in their place. To do that we need the node just before the cut (#2) and the node just after it (#5). prev starts on the head.

▼ prev: node a-1▼ after: node b+1▲ tail: end of list2■ cut a..b
nullnull10#01#113#26#39#45#5100000010000011000002prev

Nodes #3..#4 to remove (2)

[6, 9]

Variables
VariableValue
a3
b4
prev10 (#0)
after-
tail-
DepthFunction Call
Stack empty
0/12