Programming/1 Day 1 Commit [LeetCode] Add Two Numbers (Python3) - Add Two Numbers - LeetCode 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 as leetcode.com 생각보다 어려운 문제에 당황.. 결국 solution을 그대로 보고 풀었는데 그래도 이해가 안가서 2차 당황.. # 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]: dummyHead = ListNode(0) curr = dummyHead carry = 0 while l1 != None or l2 != None or carry != 0: l1Val = l1.val if l1 else 0 l2Val = l2.val if l2 else 0 columnSum = l1Val + l2Val + carry # 7 10 8 carry = columnSum // 10 # 0 1 0 # print(columnSum, carry) newNode = ListNode(columnSum % 10) #ListNode{val: 7, next: None} ListNode{val: 0, next: None} ListNode{val: 8, next: None} # print(newNode) curr.next = newNode # None None None curr = newNode # ListNode{val: 7, next: None} ListNode{val: 0, next: None} ListNode{val: 8, next: None} # print(curr.next, curr) l1 = l1.next if l1 else None # print(l1) l2 = l2.next if l2 else None # print(l2) return dummyHead.next 아직까지 고민한 흔적들이 보인다. 아무래도 파이썬으로 알고리즘 공부를 좀 해야 할까? ㅠ GitHub - Park-Minjoo/CODINGINTERVIEW_PRACTICE: 1 Day 1 Problem since 2022.4.7 1 Day 1 Problem since 2022.4.7. Contribute to Park-Minjoo/CODINGINTERVIEW_PRACTICE development by creating an account on GitHub. github.com 공유하기 게시글 관리 Mandy World 저작자표시 비영리 변경금지 'Programming > 1 Day 1 Commit' 카테고리의 다른 글 [Programmers] 자연수 뒤집어 배열로 만들기 (0) 2023.02.10 [HackerRank] Birthday Cake Candles (Python3) (0) 2023.02.06 [BaekJoon] 17413번: 단어뒤집기 2 (Python3) (0) 2023.01.27 [HackerRank] Mini-Max Sum (Python3) (0) 2023.01.26 [LeetCode] Longest Common Prefix (Python3) (0) 2023.01.25 Contents 당신이 좋아할만한 콘텐츠 [Programmers] 자연수 뒤집어 배열로 만들기 2023.02.10 [HackerRank] Birthday Cake Candles (Python3) 2023.02.06 [BaekJoon] 17413번: 단어뒤집기 2 (Python3) 2023.01.27 [HackerRank] Mini-Max Sum (Python3) 2023.01.26 댓글 0 + 이전 댓글 더보기