Skip to main content
LeetCode 1669, Medium. Topics: Linked List. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

Problem

You are given two linked lists: list1 and list2 of sizes n and m respectively. Remove list1’s nodes from the a<sup>th</sup> node to the b<sup>th</sup> node, and put list2 in their place. The blue edges and nodes in the following figure indicate the result: Example 1 Build the result list and return its head.

Examples

Example 1
Example 2

Constraints

  • 3 <= list1.length <= 10^4
  • 1 <= a <= b < list1.length - 1
  • 1 <= list2.length <= 10^4

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026