見出し画像

ほぼ日刊競プロ leetcode 2. Add Two Numbers

2. Add Two Numbers

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.

考えたこと

再帰関数で最初考えたがうまくいかず,以下のサイトを参考にした.

LeetCode Problem 2: Add Two Numbers Solution in Python

無念...

# 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]:
       carry = 0
       result = ListNode(0)
       pointer = result
       while (l1 or l2 or carry):
           first_num = l1.val if l1 else 0
           second_num = l2.val if l2 else 0
           #繰り上がりの値をたす
           summation = first_num + second_num + carry
           #10で割ったあまりを求める
           num = summation % 10
           #10で割ったものが次のノードに足されるので10で割る
           carry = summation // 10
           #次のpointerを連結リストで初期化
           pointer.next = ListNode(num)
           #ポインターを次へ移す
           pointer = pointer.next
           
           l1 = l1.next if l1 else None
           l2 = l2.next if l2 else None
           
       return result.next


この記事が気に入ったらサポートをしてみませんか?