Skip to content

Commit 8fbb32a

Browse files
authored
Merge pull request #1524 from ivanpenaloza/january22
adding algo
2 parents dd4894f + 3c5b8b5 commit 8fbb32a

5 files changed

Lines changed: 154 additions & 9 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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(object):
11+
def addTwoNumbers(self, l1, l2):
12+
"""
13+
:type l1: ListNode
14+
:type l2: ListNode
15+
:rtype: ListNode
16+
"""
17+
dummy = ListNode(0)
18+
current = dummy
19+
carry = 0
20+
21+
while l1 or l2 or carry:
22+
# Get values from current nodes (0 if node is None)
23+
val1 = l1.val if l1 else 0
24+
val2 = l2.val if l2 else 0
25+
26+
# Calculate sum and carry
27+
total = val1 + val2 + carry
28+
carry = total // 10
29+
digit = total % 10
30+
31+
# Create new node with the digit
32+
current.next = ListNode(digit)
33+
current = current.next
34+
35+
# Move to next nodes if available
36+
l1 = l1.next if l1 else None
37+
l2 = l2.next if l2 else None
38+
39+
return dummy.next
40+
41+
42+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class ListNode {
2+
val: number
3+
next: ListNode | null
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = (val===undefined ? 0 : val)
6+
this.next = (next===undefined ? null : next)
7+
}
8+
}

src/my_project/interviews_typescript/top_150_questions_round_1/ex_55_linked_list_cycle.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
class ListNode {
2-
val: number
3-
next: ListNode | null
4-
constructor(val?: number, next?: ListNode | null) {
5-
this.val = (val===undefined ? 0 : val)
6-
this.next = (next===undefined ? null : next)
7-
}
8-
}
9-
1+
import { ListNode } from './ListNode';
102

113
function hasCycle(head: ListNode | null): boolean {
124
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ListNode } from './ListNode';
2+
3+
function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
4+
const dummy = new ListNode(0);
5+
let current = dummy;
6+
let carry = 0;
7+
8+
while (l1 || l2 || carry) {
9+
// Get values from current nodes (0 if node is null)
10+
const val1 = l1 ? l1.val : 0;
11+
const val2 = l2 ? l2.val : 0;
12+
13+
// Calculate sum and carry
14+
const total = val1 + val2 + carry;
15+
carry = Math.floor(total / 10);
16+
const digit = total % 10;
17+
18+
// Create new node with the digit
19+
current.next = new ListNode(digit);
20+
current = current.next;
21+
22+
// Move to next nodes if available
23+
l1 = l1 ? l1.next : null;
24+
l2 = l2 ? l2.next : null;
25+
}
26+
27+
return dummy.next;
28+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import unittest
2+
from src.my_project.interviews.top_150_questions_round_22\
3+
.ex_56_add_two_numbers import Solution, ListNode
4+
5+
6+
class AddTwoLinkedListsTestCase(unittest.TestCase):
7+
8+
def create_linked_list(self, values):
9+
"""
10+
Helper function to create a linked list from a list of values.
11+
12+
:param values: List of node values
13+
:return: Head of the linked list
14+
"""
15+
if not values:
16+
return None
17+
18+
head = ListNode(values[0])
19+
current = head
20+
21+
for val in values[1:]:
22+
current.next = ListNode(val)
23+
current = current.next
24+
25+
return head
26+
27+
def linked_list_to_list(self, head):
28+
"""
29+
Helper function to convert linked list to Python list.
30+
31+
:param head: Head of the linked list
32+
:return: List of values
33+
"""
34+
result = []
35+
current = head
36+
37+
while current:
38+
result.append(current.val)
39+
current = current.next
40+
41+
return result
42+
43+
def test_first_pattern(self):
44+
# Input: l1 = [2,4,3], l2 = [5,6,4]
45+
# Output: [7,0,8]
46+
# Explanation: 342 + 465 = 807
47+
solution = Solution()
48+
l1 = self.create_linked_list([2, 4, 3])
49+
l2 = self.create_linked_list([5, 6, 4])
50+
output = solution.addTwoNumbers(l1, l2)
51+
result = self.linked_list_to_list(output)
52+
target = [7, 0, 8]
53+
self.assertEqual(result, target)
54+
55+
def test_second_pattern(self):
56+
# Input: l1 = [0], l2 = [0]
57+
# Output: [0]
58+
solution = Solution()
59+
l1 = self.create_linked_list([0])
60+
l2 = self.create_linked_list([0])
61+
output = solution.addTwoNumbers(l1, l2)
62+
result = self.linked_list_to_list(output)
63+
target = [0]
64+
self.assertEqual(result, target)
65+
66+
def test_third_pattern(self):
67+
# Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
68+
# Output: [8,9,9,9,0,0,0,1]
69+
solution = Solution()
70+
l1 = self.create_linked_list([9, 9, 9, 9, 9, 9, 9])
71+
l2 = self.create_linked_list([9, 9, 9, 9])
72+
output = solution.addTwoNumbers(l1, l2)
73+
result = self.linked_list_to_list(output)
74+
target = [8, 9, 9, 9, 0, 0, 0, 1]
75+
self.assertEqual(result, target)

0 commit comments

Comments
 (0)