diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_57_merge_two_sorted_linked_lists.py b/src/my_project/interviews/top_150_questions_round_22/ex_57_merge_two_sorted_linked_lists.py new file mode 100644 index 00000000..0fef82fa --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_57_merge_two_sorted_linked_lists.py @@ -0,0 +1,32 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +# Definition for singly-linked list. +class ListNode: + def __init__(self, x): + self.val = x + self.next = None + +class Solution: + def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: + # Create a dummy node to simplify edge cases + dummy = ListNode(0) + current = dummy + + # Traverse both lists and merge + while l1 and l2: + if l1.val <= l2.val: + current.next = l1 + l1 = l1.next + else: + current.next = l2 + l2 = l2.next + current = current.next + + # Attach remaining nodes from either list + if l1: + current.next = l1 + if l2: + current.next = l2 + + return dummy.next \ No newline at end of file diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_57_merge_two_sorted_linked_lists.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_57_merge_two_sorted_linked_lists.ts new file mode 100644 index 00000000..a04b6806 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_57_merge_two_sorted_linked_lists.ts @@ -0,0 +1,29 @@ +import { ListNode } from './ListNode'; + +function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null { + // Create a dummy node to simplify edge cases + const dummy = new ListNode(0); + let current = dummy; + + // Traverse both lists and merge + while (list1 && list2) { + if (list1.val <= list2.val) { + current.next = list1; + list1 = list1.next; + } else { + current.next = list2; + list2 = list2.next; + } + current = current.next; + } + + // Attach remaining nodes from either list + if (list1) { + current.next = list1; + } + if (list2) { + current.next = list2; + } + + return dummy.next; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_57_merge_two_sorted_linked_lists_round_22.py b/tests/test_150_questions_round_22/test_57_merge_two_sorted_linked_lists_round_22.py new file mode 100644 index 00000000..fa4af036 --- /dev/null +++ b/tests/test_150_questions_round_22/test_57_merge_two_sorted_linked_lists_round_22.py @@ -0,0 +1,73 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_57_merge_two_sorted_linked_lists import Solution, ListNode + +class MergeTwoLinkedListsTestCase(unittest.TestCase): + + def create_linked_list(self, values): + """ + Helper function to create a linked list from a list of values. + + :param values: List of node values + :return: Head of the linked list + """ + if not values: + return None + + head = ListNode(values[0]) + current = head + + for val in values[1:]: + current.next = ListNode(val) + current = current.next + + return head + + def linked_list_to_list(self, head): + """ + Helper function to convert linked list to Python list. + + :param head: Head of the linked list + :return: List of values + """ + result = [] + current = head + + while current: + result.append(current.val) + current = current.next + + return result + + def test_first_pattern(self): + # Input: list1 = [1,2,4], list2 = [1,3,4] + # Output: [1,1,2,3,4,4] + solution = Solution() + l1 = self.create_linked_list([1, 2, 4]) + l2 = self.create_linked_list([1, 3, 4]) + output = solution.mergeTwoLists(l1, l2) + result = self.linked_list_to_list(output) + target = [1, 1, 2, 3, 4, 4] + self.assertEqual(result, target) + + def test_second_pattern(self): + # Input: list1 = [], list2 = [] + # Output: [] + solution = Solution() + l1 = self.create_linked_list([]) + l2 = self.create_linked_list([]) + output = solution.mergeTwoLists(l1, l2) + result = self.linked_list_to_list(output) + target = [] + self.assertEqual(result, target) + + def test_third_pattern(self): + # Input: list1 = [], list2 = [0] + # Output: [0] + solution = Solution() + l1 = self.create_linked_list([]) + l2 = self.create_linked_list([0]) + output = solution.mergeTwoLists(l1, l2) + result = self.linked_list_to_list(output) + target = [0] + self.assertEqual(result, target) \ No newline at end of file