David Babalola

Posted on

# LeetCode 2. Add Two Numbers

This year, I am solving more data structures and algorithms.
P.S.: This post was first seen on Twitter. See the link below.

The Problem is 2. Add Two Numbers from LeetCode. Check it out here.

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example 1:

Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.

Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]

Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]

Constraints:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.

#### Here is my solution:

``````    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
current_l1 = l1
current_l2 = l2
l1_list, l2_list = [], []

# Convert both LinkedLists to Python Lists
while current_l1 is not None or current_l2 is not None:
if current_l1 is not None:
l1_list.append(str(current_l1.val))
current_l1 = current_l1.next

if current_l2 is not None:
l2_list.append(str(current_l2.val))
current_l2 = current_l2.next

l1_list.reverse() # Reverse lists e.g. [2, 4, 3] -> [3, 4, 2]
l2_list.reverse() # Reverse lists e.g. [2, 4, 3] -> [3, 4, 2]

# Convert the lists to integers. e.g. [3, 4, 2] -> 342
l1_value = int(''.join(l1_list))
l2_value = int(''.join(l2_list))

sum_values = l1_value + l2_value
sum_values = str(sum_values)

# Insert the elements to LinkedList in reverse order
for digit in sum_values[-2::-1]:
current.next = ListNode(digit)
current = current.next
``````

Lines 2-3:
I create `current_l1` and `current_l2` to be nodes pointing to the heads of `l1` and `l2`, respectively.
Line 4 creates empty lists `l1_list` and `l2_list` to store each digit of `l1` and `l2`.

Lines 7-14:
I convert the linked lists `l1` and `l2` into Python lists (`l1_list` and `l2_list`) by traversing through them, extracting each digit from the linked lists and appending it to the respective lists.

Lines 16-17:
I reverse `l1_list` and `l2_list` lists. I do this because the digits are in reverse order in the linked lists

Lines 20-21:
I convert the reversed lists to integers (`l1_value` and `l2_value`) by using the join method to convert to a string and then to an integer ultimately

Lines 23-24:
I calculate the sum of both integers and then convert the sum back to a string (`sum_values`) to easily extract each digit.

Lines 27-34:
I create a head node (last digit of the sum), create a pointer to it, and iterate through the remaining digits of the sum in reverse order, creating new nodes for each digit in the sum string (sum_values). The head is then returned.

Here's the performance of my approach as of today:
Runtime: 54ms, beats 79.12% of users with Python3
Memory: 16.60MB, beats 67.14% of users with Python3

Do you have a better-performing approach? What problems would you want to see me solve?

Let me know in the comments.