[ALGORITHM] LeetCode 2. Add Two Numbers
less than 1 minute read
ALGORITHM Übung - LeetCode
문제
코드
# 나의 풀이
# 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]:
iter1 = l1
num1 = str(iter1.val)
while iter1.next:
iter1 = iter1.next
num1 = str(iter1.val) + num1
iter2 = l2
num2 = str(iter2.val)
while iter2.next:
iter2 = iter2.next
num2 = str(iter2.val) + num2
num3 = str(int(num1) + int(num2))
node_list = [ListNode(s) for s in num3[::-1]]
for i in range(len(node_list)-1):
node_list[i].next = node_list[i+1]
return node_list[0]