life-hacker avatar

LeetCode Challenge (Python3). Problem 2

life-hacker

Published: 16 Apr 2022 › Updated: 16 Apr 2022LeetCode Challenge (Python3). Problem 2

LeetCode Challenge (Python3). Problem 2

leetcode

Source

Introduction of this challenge:

I recently decided to improve my programming skills, so I started using LeetCode to achieve this goal.

However, since I don't have much programming experience and this is my first time using LeetCode, there may be some difficulties in the process, but I believe I will be able to persevere.

There are currently 2230 problems on LeetCode, and I will be working on the challenge by problem number with the goal of solving all of them.

After a week from the first problem, I finally finished problem 2. I think the biggest difficulty in solving this problem was that I was not familiar with the use of recursion, so I spent a lot of time at first to understand how it works.

After a long time of research, I finally came up with a solution to this problem, although not yet very skilled, but a great sense of accomplishment.

Let's start today's problem!


Today's problem is "2. Add Two Numbers".

Link: https://leetcode.com/problems/add-two-numbers/
Difficulty: Medium
Related Topics: Linked List Math Recursion

Problem:

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:

ex1

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.

My solution:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
        l3=ListNode()
        
        if l1 != None and l2 != None: # Determine if l1,l2 is none
            l3.val=l1.val+l2.val # Add to l3
            if l3.val>=10: # Checking for carry
                l3.val-=10
                if l1.next != None: l1.next.val+=1
                elif l2.next != None: l2.next.val+=1
                else: l1.next=ListNode(1)
            l3.next=self.addTwoNumbers(l1.next,l2.next) # recursion
        elif l1 != None: # Determine if only l2 is none
            l3.val=l1.val
            if l3.val>=10:
                l3.val-=10
                if l1.next != None: l1.next.val+=1
                else: l1.next=ListNode(1)
            l3.next=self.addTwoNumbers(l1.next,None) # recursion
        elif l2 != None: # Determine if only l1 is none
            l3.val=l2.val
            if l3.val>=10:
                l3.val-=10
                if l2.next != None: l2.next.val+=1
                else: l2.next=ListNode(1)
            l3.next=self.addTwoNumbers(None,l2.next) # recursion
        else:
            return
        return l3

After coding, I submitted my solution.

_2022_04_17_12.59.04.png

Of course I'm still a long way from the best solution, so I'll try to find a better one.


This is the end of the leetcode challenge.

Feel free to give some suggestions based on my code, whether it's a way to improve performance or shorten the code.

Thanks for stopping by here and reading my post.

Leave LeetCode Challenge (Python3). Problem 2 to:

Written by

Enjoy life and write something.

Read more #hive-169321 posts


Best Posts From life-hacker

We have not curated any of life-hacker's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From life-hacker