Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 백준
- 편스토랑
- 캐치카페
- 더현대서울 맛집
- 파이썬
- Kaggle
- ubuntu
- programmers
- 데이콘
- Baekjoon
- leetcode
- ChatGPT
- 맥북
- 프로그래머스 파이썬
- hackerrank
- AI 경진대회
- gs25
- 금융문자분석경진대회
- 코로나19
- Docker
- 자연어처리
- 프로그래머스
- dacon
- PYTHON
- SW Expert Academy
- Git
- 우분투
- github
- 편스토랑 우승상품
- Real or Not? NLP with Disaster Tweets
Archives
- Today
- Total
솜씨좋은장씨
[leetCode] 445. Add Two Numbers II (Python) 본문
728x90
반응형
You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
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: ListNode, l2: ListNode) -> ListNode:
l1_list = []
l2_list = []
while l1.next != None:
l1_list.append(str(l1.val))
l1 = l1.next
l1_list.append(str(l1.val))
while l2.next != None:
l2_list.append(str(l2.val))
l2 = l2.next
l2_list.append(str(l2.val))
l1_num = int(''.join(l1_list))
l2_num = int(''.join(l2_list))
answer_num_str_list = list(str(l1_num + l2_num))
answer = list(map(int, answer_num_str_list))
for i in range(len(answer_num_str_list)):
if i == 0:
answerList = ListNode(answer_num_str_list[i])
else:
new_node = ListNode(answer_num_str_list[i])
currNode = answerList
while currNode.next != None:
currNode = currNode.next
currNode.next = new_node
return answerList
'Programming > 코딩 1일 1문제' 카테고리의 다른 글
[leetCode] 1185. Day of the Week (Python) (0) | 2020.07.29 |
---|---|
[leetCode] 260. Single Number III (Python) (0) | 2020.07.29 |
[leetCode] 1360. Number of Days Between Two Dates (Python) (0) | 2020.07.26 |
[leetCode] 1154. Day of the Year (Python) (0) | 2020.07.25 |
[leetCode] 342. Power of Four (Python) (0) | 2020.07.24 |
Comments