From 3c5b8b51a3dcdfb05133539183ba8ed2397f7481 Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 22 Jan 2026 04:21:03 -0600 Subject: [PATCH] adding algo --- .../ex_56_add_two_numbers.py | 42 +++++++++++ .../top_150_questions_round_1/ListNode.ts | 8 ++ .../ex_55_linked_list_cycle.ts | 10 +-- .../ex_56_add_two_numbers.ts | 28 +++++++ .../test_56_add_two_numbers_round_22.py | 75 +++++++++++++++++++ 5 files changed, 154 insertions(+), 9 deletions(-) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_56_add_two_numbers.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ListNode.ts create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_56_add_two_numbers.ts create mode 100644 tests/test_150_questions_round_22/test_56_add_two_numbers_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_56_add_two_numbers.py b/src/my_project/interviews/top_150_questions_round_22/ex_56_add_two_numbers.py new file mode 100644 index 00000000..4c4a1b19 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_56_add_two_numbers.py @@ -0,0 +1,42 @@ +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(object): + def addTwoNumbers(self, l1, l2): + """ + :type l1: ListNode + :type l2: ListNode + :rtype: ListNode + """ + dummy = ListNode(0) + current = dummy + carry = 0 + + while l1 or l2 or carry: + # Get values from current nodes (0 if node is None) + val1 = l1.val if l1 else 0 + val2 = l2.val if l2 else 0 + + # Calculate sum and carry + total = val1 + val2 + carry + carry = total // 10 + digit = total % 10 + + # Create new node with the digit + current.next = ListNode(digit) + current = current.next + + # Move to next nodes if available + l1 = l1.next if l1 else None + l2 = l2.next if l2 else None + + return dummy.next + + + diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ListNode.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ListNode.ts new file mode 100644 index 00000000..91741114 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ListNode.ts @@ -0,0 +1,8 @@ +export class ListNode { + val: number + next: ListNode | null + constructor(val?: number, next?: ListNode | null) { + this.val = (val===undefined ? 0 : val) + this.next = (next===undefined ? null : next) + } +} diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_55_linked_list_cycle.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_55_linked_list_cycle.ts index 123983f1..7df76e11 100644 --- a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_55_linked_list_cycle.ts +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_55_linked_list_cycle.ts @@ -1,12 +1,4 @@ -class ListNode { - val: number - next: ListNode | null - constructor(val?: number, next?: ListNode | null) { - this.val = (val===undefined ? 0 : val) - this.next = (next===undefined ? null : next) - } -} - +import { ListNode } from './ListNode'; function hasCycle(head: ListNode | null): boolean { /** diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_56_add_two_numbers.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_56_add_two_numbers.ts new file mode 100644 index 00000000..010fb277 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_56_add_two_numbers.ts @@ -0,0 +1,28 @@ +import { ListNode } from './ListNode'; + +function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null { + const dummy = new ListNode(0); + let current = dummy; + let carry = 0; + + while (l1 || l2 || carry) { + // Get values from current nodes (0 if node is null) + const val1 = l1 ? l1.val : 0; + const val2 = l2 ? l2.val : 0; + + // Calculate sum and carry + const total = val1 + val2 + carry; + carry = Math.floor(total / 10); + const digit = total % 10; + + // Create new node with the digit + current.next = new ListNode(digit); + current = current.next; + + // Move to next nodes if available + l1 = l1 ? l1.next : null; + l2 = l2 ? l2.next : null; + } + + return dummy.next; +} \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_56_add_two_numbers_round_22.py b/tests/test_150_questions_round_22/test_56_add_two_numbers_round_22.py new file mode 100644 index 00000000..8d7f9c12 --- /dev/null +++ b/tests/test_150_questions_round_22/test_56_add_two_numbers_round_22.py @@ -0,0 +1,75 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_56_add_two_numbers import Solution, ListNode + + +class AddTwoLinkedListsTestCase(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: l1 = [2,4,3], l2 = [5,6,4] + # Output: [7,0,8] + # Explanation: 342 + 465 = 807 + solution = Solution() + l1 = self.create_linked_list([2, 4, 3]) + l2 = self.create_linked_list([5, 6, 4]) + output = solution.addTwoNumbers(l1, l2) + result = self.linked_list_to_list(output) + target = [7, 0, 8] + self.assertEqual(result, target) + + def test_second_pattern(self): + # Input: l1 = [0], l2 = [0] + # Output: [0] + solution = Solution() + l1 = self.create_linked_list([0]) + l2 = self.create_linked_list([0]) + output = solution.addTwoNumbers(l1, l2) + result = self.linked_list_to_list(output) + target = [0] + self.assertEqual(result, target) + + def test_third_pattern(self): + # Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] + # Output: [8,9,9,9,0,0,0,1] + solution = Solution() + l1 = self.create_linked_list([9, 9, 9, 9, 9, 9, 9]) + l2 = self.create_linked_list([9, 9, 9, 9]) + output = solution.addTwoNumbers(l1, l2) + result = self.linked_list_to_list(output) + target = [8, 9, 9, 9, 0, 0, 0, 1] + self.assertEqual(result, target) \ No newline at end of file