Skip to content

Commit ba6e473

Browse files
committed
Merge in between linked lists
1 parent a575cb0 commit ba6e473

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 1669. Merge in between linked lists
2+
# Topics: 'Linked List'
3+
# Level: 'Medium'
4+
5+
# You are given two linked lists: list1 and list2 of sizes n and m respectively.
6+
7+
# Remove list1's nodes from the ath node to the bth node, and put list2 in their place.
8+
9+
# The blue edges and nodes in the following figure indicate the result:
10+
11+
# Build the result list and return its head.
12+
13+
# Example 1:
14+
15+
# Input: list1 = [10,1,13,6,9,5], a = 3, b = 4, list2 = [1000000,1000001,1000002]
16+
# Output: [10,1,13,1000000,1000001,1000002,5]
17+
# Explanation: We remove the nodes 3 and 4 and put the entire list2 in their place. The blue edges and nodes in the above figure indicate the result.
18+
19+
# Example 2:
20+
21+
# Input: list1 = [0,1,2,3,4,5,6], a = 2, b = 5, list2 = [1000000,1000001,1000002,1000003,1000004]
22+
# Output: [0,1,1000000,1000001,1000002,1000003,1000004,6]
23+
# Explanation: The blue edges and nodes in the above figure indicate the result.
24+
25+
26+
27+
# Constraints:
28+
29+
# 3 <= list1.length <= 104
30+
# 1 <= a <= b < list1.length - 1
31+
# 1 <= list2.length <= 104
32+
33+
34+
class ListNode:
35+
def __init__(self, val=0, next=None):
36+
self.val = val
37+
self.next = next
38+
39+
class Solution:
40+
def mergeInBetween(self, list1: ListNode, a: int, b: int, list2: ListNode) -> ListNode:
41+
dummy = ListNode()
42+
cur = list1
43+
dcur = dummy
44+
for i in range (a):
45+
dcur.next = cur
46+
dcur = dcur.next
47+
cur = cur.next
48+
curend = list1
49+
for i in range (b):
50+
curend = curend.next
51+
dcur.next = list2
52+
while dcur.next:
53+
dcur = dcur.next
54+
dcur.next = curend.next
55+
return dummy.next
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from merge import ListNode
2+
from merge import Solution
3+
4+
# Helper functions
5+
def create_linked_list(arr):
6+
"""Create a linked list from an array"""
7+
if not arr:
8+
return None
9+
head = ListNode(arr[0])
10+
current = head
11+
for val in arr[1:]:
12+
current.next = ListNode(val)
13+
current = current.next
14+
return head
15+
16+
def linked_list_to_array(head):
17+
"""Convert a linked list to an array"""
18+
result = []
19+
current = head
20+
while current:
21+
result.append(current.val)
22+
current = current.next
23+
return result
24+
25+
# Test cases
26+
def test_example_1():
27+
"""Example 1: Remove middle nodes and insert list2"""
28+
sol = Solution()
29+
list1 = create_linked_list([10, 1, 13, 6, 9, 5])
30+
list2 = create_linked_list([1000000, 1000001, 1000002])
31+
result = sol.mergeInBetween(list1, 3, 4, list2)
32+
expected = [10, 1, 13, 1000000, 1000001, 1000002, 5]
33+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
34+
print("✓ Test 1 passed: Example 1")
35+
36+
def test_example_2():
37+
"""Example 2: Remove larger range"""
38+
sol = Solution()
39+
list1 = create_linked_list([0, 1, 2, 3, 4, 5, 6])
40+
list2 = create_linked_list([1000000, 1000001, 1000002, 1000003, 1000004])
41+
result = sol.mergeInBetween(list1, 2, 5, list2)
42+
expected = [0, 1, 1000000, 1000001, 1000002, 1000003, 1000004, 6]
43+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
44+
print("✓ Test 2 passed: Example 2")
45+
46+
def test_remove_single_node():
47+
"""Remove a single node (a == b)"""
48+
sol = Solution()
49+
list1 = create_linked_list([1, 2, 3, 4, 5])
50+
list2 = create_linked_list([10, 20])
51+
result = sol.mergeInBetween(list1, 2, 2, list2)
52+
expected = [1, 2, 10, 20, 4, 5]
53+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
54+
print("✓ Test 3 passed: Remove single node")
55+
56+
def test_remove_at_start():
57+
"""Remove nodes starting from index 1"""
58+
sol = Solution()
59+
list1 = create_linked_list([1, 2, 3, 4, 5])
60+
list2 = create_linked_list([100, 200])
61+
result = sol.mergeInBetween(list1, 1, 2, list2)
62+
expected = [1, 100, 200, 4, 5]
63+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
64+
print("✓ Test 4 passed: Remove at start")
65+
66+
def test_remove_till_end():
67+
"""Remove nodes till near the end"""
68+
sol = Solution()
69+
list1 = create_linked_list([1, 2, 3, 4, 5, 6])
70+
list2 = create_linked_list([100])
71+
result = sol.mergeInBetween(list1, 2, 4, list2)
72+
expected = [1, 2, 100, 6]
73+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
74+
print("✓ Test 5 passed: Remove till near end")
75+
76+
def test_single_node_list2():
77+
"""list2 has only one node"""
78+
sol = Solution()
79+
list1 = create_linked_list([1, 2, 3, 4, 5])
80+
list2 = create_linked_list([99])
81+
result = sol.mergeInBetween(list1, 1, 3, list2)
82+
expected = [1, 99, 5]
83+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
84+
print("✓ Test 6 passed: Single node list2")
85+
86+
def test_long_list2():
87+
"""list2 is longer than the removed section"""
88+
sol = Solution()
89+
list1 = create_linked_list([1, 2, 3, 4])
90+
list2 = create_linked_list([10, 20, 30, 40, 50])
91+
result = sol.mergeInBetween(list1, 1, 1, list2)
92+
expected = [1, 10, 20, 30, 40, 50, 3, 4]
93+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
94+
print("✓ Test 7 passed: Long list2")
95+
96+
def test_minimum_list1():
97+
"""Minimum size list1 (3 nodes)"""
98+
sol = Solution()
99+
list1 = create_linked_list([1, 2, 3])
100+
list2 = create_linked_list([100])
101+
result = sol.mergeInBetween(list1, 1, 1, list2)
102+
expected = [1, 100, 3]
103+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
104+
print("✓ Test 8 passed: Minimum list1")
105+
106+
def test_large_values():
107+
"""Test with large values"""
108+
sol = Solution()
109+
list1 = create_linked_list([0, 1, 2, 3, 4])
110+
list2 = create_linked_list([1000000, 9999999])
111+
result = sol.mergeInBetween(list1, 2, 3, list2)
112+
expected = [0, 1, 1000000, 9999999, 4]
113+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
114+
print("✓ Test 9 passed: Large values")
115+
116+
def test_consecutive_range():
117+
"""Remove consecutive nodes in middle"""
118+
sol = Solution()
119+
list1 = create_linked_list([5, 10, 15, 20, 25, 30])
120+
list2 = create_linked_list([100, 200, 300])
121+
result = sol.mergeInBetween(list1, 2, 3, list2)
122+
expected = [5, 10, 100, 200, 300, 25, 30]
123+
assert linked_list_to_array(result) == expected, f"Expected {expected}, got {linked_list_to_array(result)}"
124+
print("✓ Test 10 passed: Consecutive range")
125+
126+
# Run all tests
127+
if __name__ == "__main__":
128+
print("Running tests...\n")
129+
test_example_1()
130+
test_example_2()
131+
test_remove_single_node()
132+
test_remove_at_start()
133+
test_remove_till_end()
134+
test_single_node_list2()
135+
test_long_list2()
136+
test_minimum_list1()
137+
test_large_values()
138+
test_consecutive_range()
139+
print("\n✅ All tests passed!")

0 commit comments

Comments
 (0)