Skip to content

Instantly share code, notes, and snippets.

@RaminMammadzada
Created December 3, 2021 11:04
Show Gist options
  • Save RaminMammadzada/e79093a3cdc124aed352d0b36340a80e to your computer and use it in GitHub Desktop.
Save RaminMammadzada/e79093a3cdc124aed352d0b36340a80e to your computer and use it in GitHub Desktop.
CodeSignal-addTwoHugeNumbers
You're given 2 huge integers represented by linked lists. Each linked list element is a number from 0 to 9999 that represents a number with exactly 4 digits. The represented number might have leading zeros. Your task is to add up these huge integers and return the result in the same format.
Example
For a = [9876, 5432, 1999] and b = [1, 8001], the output should be
solution(a, b) = [9876, 5434, 0].
Explanation: 987654321999 + 18001 = 987654340000.
For a = [123, 4, 5] and b = [100, 100, 100], the output should be
solution(a, b) = [223, 104, 105].
Explanation: 12300040005 + 10001000100 = 22301040105.
Input/Output
[execution time limit] 4 seconds (py3)
[input] linkedlist.integer a
The first number, without its leading zeros.
Guaranteed constraints:
0 ≤ a size ≤ 104,
0 ≤ element value ≤ 9999.
[input] linkedlist.integer b
The second number, without its leading zeros.
Guaranteed constraints:
0 ≤ b size ≤ 104,
0 ≤ element value ≤ 9999.
[output] linkedlist.integer
The result of adding a and b together, returned without leading zeros in the same format.
[Python 3] Syntax Tips
# Prints help message to the console
# Returns a string
def helloWorld(name):
print("This prints to the console when you Run Tests")
return "Hello, " + name
@RaminMammadzada
Copy link
Author

# Singly-linked lists are already defined with this interface:
# class ListNode(object):
#   def __init__(self, x):
#     self.value = x
#     self.next = None
#
def solution(a, b):
    
    num1String = ""
    
    while a:
        currValue = str(a.value)
        if len(currValue) < 4:
            currValue = "0"*(4 - len(currValue) ) + currValue
        num1String += currValue
        a = a.next
    
    num2String = ""
    while b:
        currValue = str(b.value)
        if len(currValue) < 4:
            currValue = "0"*(4 - len(currValue) ) + currValue
        num2String += currValue
        b = b.next
    
    total = int(num1String) + int(num2String)
    totalString = str(total)
    print("num1String: ", num1String)
    print("num2String: ", num2String)
    print("totalString before: ", totalString)
    
    if len(totalString) % 4 > 0:
        totalString = "0" * (4 - len(totalString) % 4) + totalString
    
    print("totalString after: ", totalString)
    
    head = None
    
    for i in range(len(totalString), 0, -4):
        currValue = totalString[i-4: i]
        currValue = int(currValue)
        print("currValue: ", currValue)
        print("i: ", i)
        
        nextNode = ListNode(currValue)
        nextNode.next = head
        head = nextNode
    
    return head

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment