FastPrepMerge One List Between Two Positions

Merge One List Between Two Positions

Oracle logoOracle● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

list1 and list2 contain linked-list node values in traversal order. Remove the inclusive range of nodes from index a through index b in list1, and splice all nodes of list2 in their place.

Return the resulting node values.

Function

mergeInBetween(list1: int[], a: int, b: int, list2: int[]) → int[]

Examples

Example 1

list1 = [0,1,2,3,4,5]a = 3b = 4list2 = [1000000,1000001,1000002]return = [0,1,2,1000000,1000001,1000002,5]

Values at indices 3 and 4 are replaced by the second list.

Example 2

list1 = [1,2,3]a = 0b = 2list2 = [9]return = [9]

Replacing the full first list leaves only the inserted list.

Constraints

  • 1 <= list1.length <= 100000.
  • 0 <= a <= b < list1.length.
  • 0 <= list2.length <= 100000.

More Oracle problems

See Oracle hiring insights
public int[] mergeInBetween(int[] list1, int a, int b, int[] list2) {
  // write your code here
}
list1[0,1,2,3,4,5]
a3
b4
list2[1000000,1000001,1000002]
expected[0,1,2,1000000,1000001,1000002,5]
Checking account…