[Medium] 1669. Merge In Between Linked Lists
[Medium] 1669. Merge In Between Linked Lists
You are given two singly linked lists, list1 and list2, and two integers a and b.
You must:
- remove nodes from index
atob(inclusive) inlist1 - insert
list2into that gap
Key Insight (ACM-style thinking)
This is pure pointer rewiring.
Find:
prevA: node right before indexa(a - 1)afterB: node right after indexb(b + 1)
Then connect:
prevA -> list2 -> afterB
Step-by-step Plan
- Traverse
list1to locateprevA. - Continue to locate
afterB. - Traverse
list2to locate its tail. - Rewire pointers:
prevA.next = list2tail.next = afterB
Complexity
- Time:
O(n + m) - Space:
O(1)
Solution
from typing import Optional
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def mergeInBetween(
self, list1: "ListNode", a: int, b: int, list2: "ListNode"
) -> "ListNode":
dummy = ListNode(0, list1)
prevA = dummy
# Find node before index a.
for _ in range(a):
prevA = prevA.next
# Move to node at index b, then step once more to index b+1.
afterB = prevA
for _ in range(b - a + 1):
afterB = afterB.next
afterB = afterB.next
# Attach list2 at cut position.
prevA.next = list2
# Find tail of list2.
tail = list2
while tail.next:
tail = tail.next
# Connect tail of list2 to remainder of list1.
tail.next = afterB
return dummy.next