Skip to content

Commit a600ee9

Browse files
authored
Merge pull request #1525 from ivanpenaloza/january23
adding algo
2 parents 8fbb32a + d8e76ff commit a600ee9

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import List, Union, Collection, Mapping, Optional
2+
from abc import ABC, abstractmethod
3+
4+
# Definition for singly-linked list.
5+
class ListNode:
6+
def __init__(self, x):
7+
self.val = x
8+
self.next = None
9+
10+
class Solution:
11+
def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
12+
# Create a dummy node to simplify edge cases
13+
dummy = ListNode(0)
14+
current = dummy
15+
16+
# Traverse both lists and merge
17+
while l1 and l2:
18+
if l1.val <= l2.val:
19+
current.next = l1
20+
l1 = l1.next
21+
else:
22+
current.next = l2
23+
l2 = l2.next
24+
current = current.next
25+
26+
# Attach remaining nodes from either list
27+
if l1:
28+
current.next = l1
29+
if l2:
30+
current.next = l2
31+
32+
return dummy.next
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { ListNode } from './ListNode';
2+
3+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
4+
// Create a dummy node to simplify edge cases
5+
const dummy = new ListNode(0);
6+
let current = dummy;
7+
8+
// Traverse both lists and merge
9+
while (list1 && list2) {
10+
if (list1.val <= list2.val) {
11+
current.next = list1;
12+
list1 = list1.next;
13+
} else {
14+
current.next = list2;
15+
list2 = list2.next;
16+
}
17+
current = current.next;
18+
}
19+
20+
// Attach remaining nodes from either list
21+
if (list1) {
22+
current.next = list1;
23+
}
24+
if (list2) {
25+
current.next = list2;
26+
}
27+
28+
return dummy.next;
29+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_57_merge_two_sorted_linked_lists import Solution, ListNode
4+
5+
class MergeTwoLinkedListsTestCase(unittest.TestCase):
6+
7+
def create_linked_list(self, values):
8+
"""
9+
Helper function to create a linked list from a list of values.
10+
11+
:param values: List of node values
12+
:return: Head of the linked list
13+
"""
14+
if not values:
15+
return None
16+
17+
head = ListNode(values[0])
18+
current = head
19+
20+
for val in values[1:]:
21+
current.next = ListNode(val)
22+
current = current.next
23+
24+
return head
25+
26+
def linked_list_to_list(self, head):
27+
"""
28+
Helper function to convert linked list to Python list.
29+
30+
:param head: Head of the linked list
31+
:return: List of values
32+
"""
33+
result = []
34+
current = head
35+
36+
while current:
37+
result.append(current.val)
38+
current = current.next
39+
40+
return result
41+
42+
def test_first_pattern(self):
43+
# Input: list1 = [1,2,4], list2 = [1,3,4]
44+
# Output: [1,1,2,3,4,4]
45+
solution = Solution()
46+
l1 = self.create_linked_list([1, 2, 4])
47+
l2 = self.create_linked_list([1, 3, 4])
48+
output = solution.mergeTwoLists(l1, l2)
49+
result = self.linked_list_to_list(output)
50+
target = [1, 1, 2, 3, 4, 4]
51+
self.assertEqual(result, target)
52+
53+
def test_second_pattern(self):
54+
# Input: list1 = [], list2 = []
55+
# Output: []
56+
solution = Solution()
57+
l1 = self.create_linked_list([])
58+
l2 = self.create_linked_list([])
59+
output = solution.mergeTwoLists(l1, l2)
60+
result = self.linked_list_to_list(output)
61+
target = []
62+
self.assertEqual(result, target)
63+
64+
def test_third_pattern(self):
65+
# Input: list1 = [], list2 = [0]
66+
# Output: [0]
67+
solution = Solution()
68+
l1 = self.create_linked_list([])
69+
l2 = self.create_linked_list([0])
70+
output = solution.mergeTwoLists(l1, l2)
71+
result = self.linked_list_to_list(output)
72+
target = [0]
73+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)