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