728x90
https://leetcode.com/problems/add-two-numbers/description/
링크드 리스트를 받아서 숫자를 추출해 더해준후
뒤집어서 제출하면 되는 문제
미디움은 아닌듯...?
class Solution:
def addTwoNumbers(self, l1: Optional[ListNode], l2: Optional[ListNode]) -> Optional[ListNode]:
str_l1 = ""
str_l2 = ""
while l1:
str_l1 = str(l1.val) + str_l1
l1 = l1.next
while l2:
str_l2 = str(l2.val) + str_l2
l2 = l2.next
result = str(int(str_l1) + int(str_l2))
list_node = None
result_node = None
for re in reversed(result):
if not result_node:
result_node = ListNode(int(re))
list_node = result_node
else:
list_node.next = ListNode(int(re))
list_node = list_node.next
return list_node
728x90
'PS > 릿코드' 카테고리의 다른 글
[릿코드 122] Best Time to Buy and Sell Stock II (파이썬/python) (0) | 2023.03.07 |
---|---|
[릿코드 2187] Minimum Time to Complete Trips (파이썬/python) (0) | 2023.03.07 |
[릿코드 873] Length of Longest Fibonacci Subsequence (파이썬/python) (0) | 2023.03.03 |
[릿코드 1557] Minimum Number of Vertices to Reach All Nodes (파이썬/python) (0) | 2023.03.02 |
[릿코드 638] Shopping Offers (파이썬/python) (0) | 2023.02.23 |