일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- Python Code
- Substring with Concatenation of All Words
- 컴퓨터의 구조
- 프로그래머스
- 운영체제
- shiba
- 파이썬
- LeetCode
- 109. Convert Sorted List to Binary Search Tree
- 715. Range Module
- 315. Count of Smaller Numbers After Self
- 시바견
- DWG
- Class
- Decorator
- kaggle
- iterator
- Regular Expression
- Convert Sorted List to Binary Search Tree
- 30. Substring with Concatenation of All Words
- data science
- t1
- Protocol
- Python
- 밴픽
- Python Implementation
- Generator
- concurrency
- 43. Multiply Strings
- attribute
Archives
- Today
- Total
Scribbling
[C++ Reminder] LeetCode 2. Add Two Numbers 본문
/**
* 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* curPtr = new ListNode(0);
ListNode* ret = curPtr;
int sum=0, carry = 0;
while (l1 || l2 || carry) {
int temp = (l1? l1->val: 0) + (l2? l2->val: 0) + carry;
curPtr->next = new ListNode(temp%10);
curPtr = curPtr->next;
l1 = l1? l1->next: l1;
l2 = l2? l2->next: l2;
carry = int(temp/10);
}
return ret->next;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] 49. Group Anagrams (0) | 2023.08.18 |
---|---|
[C++] LeetCode 242. Valid Anagram (0) | 2023.08.18 |
[C++] LeetCode 217. Contains Duplicate (0) | 2023.08.18 |
[C++ Reminder] LeetCode: 15. 3Sum (0) | 2023.01.26 |
[C++ Reminder] LeetCode 1. Two Sum (0) | 2023.01.23 |