Insert a Linked List into Another Within an Index Range

Problem Given two linked lists and positions a, b: remove the nodes in positions [a, b] of the first list and splice the entire second list into the gap.

Input / Output

  • Input: heads list1, list2; ints a <= b (0-indexed into list1).
  • Output: head of the merged list.

Constraints

  • Up to 10^4 nodes; O(m + n) single pass.
  • a >= 1 in the classic setting (head survives) — clarify, else use a dummy head.

Example

  • list1 = 0->1->2->3->4->5, a=3, b=4, list2 = 100->101->102 -> 0->1->2->100->101->102->5.
asked …
LeaderboardSalaryAccount