일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- 컴퓨터의 구조
- Decorator
- LeetCode
- concurrency
- Python
- Python Code
- shiba
- data science
- iterator
- Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- 운영체제
- attribute
- Python Implementation
- 시바견
- Regular Expression
- Class
- 밴픽
- DWG
- 109. Convert Sorted List to Binary Search Tree
- 파이썬
- Protocol
- Substring with Concatenation of All Words
- t1
- 프로그래머스
- 715. Range Module
- Generator
- 315. Count of Smaller Numbers After Self
- 30. Substring with Concatenation of All Words
Archives
- Today
- Total
Scribbling
[Java 101] LeetCode: 2. Add Two Numbers 본문
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ret = new ListNode();
ListNode cur = ret;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
if (l1 == null)
l1 = new ListNode(0);
if (l2 == null)
l2 = new ListNode(0);
int sum = l1.val + l2.val + carry;
int num = sum % 10;
carry = sum/10;
ListNode tmp = new ListNode(num);
cur.next = tmp;
cur = tmp;
l1 = l1.next;
l2 = l2.next;
}
return ret.next;
}
}
'Computer Science > Java' 카테고리의 다른 글
[Java 101] 242. Valid Anagram with HashMap (0) | 2023.02.14 |
---|---|
[Java 101] 217. Contains Duplicate with HashSet, Arrays.stream (0) | 2023.02.14 |
[Java 101] LeetCode: 15. 3Sum with 2D List (0) | 2023.02.14 |
[Java Basics2] 이것이 JAVA다 내용 정리 (0) | 2023.02.13 |
[Java Basics] Major Difference to Python, C++, and JS (0) | 2023.01.31 |