1669 Merge in Between Linked Lists
Approach 1
Nothing fancy, just some traversal and pointer manipulation.
- Initialize two dummy header nodes
- Initialize
currandprevpointers for both list1 and list2 - Advancing both
prevandcurrinlist1. Traverse such thatprevis at the node beforeaandcurris at the nodea - Advancing only
currinlist1it's at the next of node of b. Ifb == 4,curris at the node 5 - Advancing
prevandcurrinlist2untilcurris at the end of list2 - connection time!!
# 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:
dummy1,dummy2 = ListNode(0,list1),ListNode(0,list2)
curr1,curr2 = list1,list2
prev1,prev2 = dummy1,dummy2
counter = 0
while counter < a and curr1:
curr1 = curr1.next
prev1 = prev1.next
counter += 1
# when we got here
while counter <= b:
curr1 = curr1.next
counter += 1
while curr2:
prev2 = prev2.next
curr2 = curr2.next
# connect
prev1.next = list2
prev2.next = curr1
return dummy1.next