https://leetcode.com/problems/add-two-numbers/description/
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.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Constraints:
- The number of nodes in each linked list is in the range [1, 100].
- 0 <= Node.val <= 9
- It is guaranteed that the list represents a number that does not have leading zeros.
# 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,l2):
ans = ListNode()
temp = ans
carry = 0
while l1 or l2 or carry !=0 :
if l1:
d1 = l1.val
else : d1 = 0
if l2:
d2 = l2.val
else : d2 = 0
sum = d1 + d2 + carry
d = sum % 10
carry = sum // 10
newNode = ListNode(d)
temp.next = newNode
temp = temp.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
return ans.next
// C언어
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* createNode(int val) {
struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
newNode->val = val;
newNode->next = NULL;
return newNode;
}
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
struct ListNode ans;
struct ListNode* temp = &ans;
int c = 0;
int d1,d2;
while(l1 || l2 || c!=0 ){
if(l1){
d1 = l1->val;
}else d1 = 0;
if(l2){
d2 = l2->val;
}else d2 = 0;
int sum = (d1+d2+c)%10;
c = (d1+d2+c)/10;
struct ListNode *newNode = createNode(sum);
temp -> next = newNode;
temp = temp->next;
if(l1){
l1 = l1->next;
}
if(l2){
l2 = l2->next;
}
}
return ans.next;
}
C로 했을 땐 좀 귀찮게
struct Listnode* createNode(int val){
struct Listnode* newNode = (struct Listnode*)malloc(sizeof(struct Listnode));
newNode -> val = val;
newNode -> next = NULL;
return newNode;
};
이렇게 귀찮지만 함수를 선언해주는 편이 좋다.
//C++ 코드
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode* ans = new ListNode(999);
ListNode* temp = ans;
int c = 0;
while(l1||l2||c!=0){
int d1 = (l1) ? l1->val : 0;
int d2 = (l2) ? l2->val : 0;
int sum = (d1+d2+c);
int d = sum % 10;
c = sum/10;
ListNode* newNode = new ListNode(d);
temp -> next = newNode;
temp = temp -> next;
l1 = (l1) ? l1->next : nullptr;
l2 = (l2) ? l2->next : nullptr;
}
ListNode* result = ans->next;
delete ans;
return result;
}
};
'Coding_Practice' 카테고리의 다른 글
Remove Element[E,Array, Two Pointers] (0) | 2024.07.21 |
---|---|
Longest Palindromic Substring[M] (1) | 2024.07.21 |
Remove Duplicates from Sorted Array(E) (0) | 2024.07.20 |
Longest Substring Without Repeating Characters[M] (0) | 2024.07.20 |
https://github.com/bgg-lee/CS_PRACTICE/tree/main (0) | 2024.03.25 |