Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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



Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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)