Skip to content

Commit c5c67cb

Browse files
authored
Create Radix Addition
1 parent de056b1 commit c5c67cb

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

interview_query/Radix Addition

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
You are given two non-empty linked lists representing two non-negative integers. Each list contains a single number, where each item in the list is one digit. The digits are stored in reverse order.
2+
3+
Task: Add the two numbers and return the sum as a linked list, also with the digits in reverse order. You may assume the two numbers do not contain any leading zeros, except the number 0 itself.
4+
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
5+
#my solution
6+
7+
class ListNode:
8+
def __init__(self, x):
9+
self.val = x
10+
self.next = None
11+
12+
def to_list(node):
13+
"""Do not modify """
14+
"""Converts a linked list to a list of integers."""
15+
nums = []
16+
while node:
17+
nums.append(node.val)
18+
node = node.next
19+
return nums
20+
def to_linked_list(nums):
21+
"""Do not modify """
22+
"""A helper function: Converts a list of integers to a linked list and returns the head of the linked list."""
23+
if not nums:
24+
return None
25+
head = ListNode(nums[0])
26+
current = head
27+
for num in nums[1:]:
28+
current.next = ListNode(num)
29+
current = current.next
30+
return head
31+
32+
def helper(nums1, nums2):
33+
"""Do not modify """
34+
l1 = to_linked_list(nums1)
35+
l2 = to_linked_list(nums2)
36+
result = addTwoNumbers(l1,l2)
37+
return to_list(result)
38+
39+
40+
def addTwoNumbers(l1,l2):
41+
x1 = to_list(l1)
42+
x2 = to_list(l2)
43+
x1 = x1[::-1]
44+
x2 = x2[::-1]
45+
46+
x1 = list(map(str,x1))
47+
x2 = list(map(str,x2))
48+
x1 = ''.join(x1)
49+
x2 = ''.join(x2)
50+
x1 = int(x1)
51+
x2 = int(x2)
52+
res = x1+x2
53+
54+
res = list(str(res))
55+
56+
res = list(map(int, res))
57+
res = res[::-1]
58+
return to_linked_list(res)
59+
60+
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
61+
62+
We can solve this problem by traversing both lists at the same time, and adding corresponding digits. If the sum is greater than 9, we carry the 1 to the next digit. We continue this process until we have traversed both lists. If one list is longer than the other, we continue the process with the longer list. If there is a carry after the last digit, we add a new node with the carry.
63+
64+
class ListNode:
65+
def __init__(self, x):
66+
self.val = x
67+
self.next = None
68+
def to_linked_list(nums):
69+
"""Help function, do not modify"""
70+
"""A helper function: Converts a list of integers to a linked list and returns the head of the linked list."""
71+
if not nums:
72+
return None
73+
head = ListNode(nums[0])
74+
current = head
75+
for num in nums[1:]:
76+
current.next = ListNode(num)
77+
current = current.next
78+
return head
79+
80+
81+
def addTwoNumbers(nums1, nums2):
82+
l1 = to_linked_list(nums1)
83+
l2 = to_linked_list(nums2)
84+
dummy = cur = ListNode(0)
85+
carry = 0
86+
while l1 or l2 or carry:
87+
if l1:
88+
carry += l1.val
89+
l1 = l1.next
90+
if l2:
91+
carry += l2.val
92+
l2 = l2.next
93+
cur.next = ListNode(carry%10)
94+
cur = cur.next
95+
carry //= 10
96+
return dummy.next
97+

0 commit comments

Comments
 (0)