Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 973983f

Browse files
committed
feat: add leetcode question #449
1 parent 96f16d5 commit 973983f

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

‎.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,6 @@ captures/
269269
# DS_Store files
270270
*.DS_Store
271271
.MWebMetaData/
272+
273+
nohup.out
274+
leetcode/nohup.out
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.hi.dhl.algorithms.leetcode._449.java;
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2022年4月29日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
import com.hi.dhl.algorithms.model.ListNode;
12+
13+
import java.util.ArrayDeque;
14+
import java.util.Deque;
15+
16+
/**
17+
* Definition for singly-linked list.
18+
* public class ListNode {
19+
* int val;
20+
* ListNode next;
21+
* ListNode() {}
22+
* ListNode(int val) { this.val = val; }
23+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
24+
* }
25+
*/
26+
class Solution2 {
27+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
28+
Deque<Integer> stack1 = new ArrayDeque<>();
29+
Deque<Integer> stack2 = new ArrayDeque<>();
30+
31+
while (l1 != null) {
32+
stack1.push(l1.val);
33+
l1 = l1.next;
34+
}
35+
36+
while (l2 != null) {
37+
stack2.push(l2.val);
38+
l2 = l2.next;
39+
}
40+
41+
int carry = 0;
42+
ListNode head = null;
43+
while (!stack1.isEmpty() || !stack2.isEmpty() || carry > 0) {
44+
int a = !stack1.isEmpty() ? stack1.pop() : 0;
45+
int b = !stack2.isEmpty() ? stack2.pop() : 0;
46+
int sum = a + b + carry;
47+
carry = sum / 10;
48+
ListNode node = new ListNode(sum % 10);
49+
node.next = head;
50+
head = node;
51+
}
52+
return head;
53+
}
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.hi.dhl.algorithms.leetcode._449.kotlin
2+
3+
import com.hi.dhl.algorithms.model.ListNode
4+
import java.util.*
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2022年4月29日
10+
* desc :
11+
* </pre>
12+
*/
13+
/**
14+
* Example:
15+
* var li = ListNode(5)
16+
* var v = li.`val`
17+
* Definition for singly-linked list.
18+
* class ListNode(var `val`: Int) {
19+
* var next: ListNode? = null
20+
* }
21+
*/
22+
class Solution2 {
23+
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
24+
val stack1 = ArrayDeque<Int>()
25+
val stack2 = ArrayDeque<Int>()
26+
27+
var p1 = l1
28+
var p2 = l2
29+
while (p1 != null) {
30+
stack1.push(p1.`val`)
31+
p1 = p1.next
32+
}
33+
34+
while (p2 != null) {
35+
stack2.push(p2.`val`)
36+
p2 = p2.next
37+
}
38+
39+
var carry = 0
40+
var head: ListNode? = null
41+
while (!stack1.isEmpty() || !stack2.isEmpty() || carry > 0) {
42+
val a = if (!stack1.isEmpty()) stack1.pop() else 0
43+
val b = if (!stack2.isEmpty()) stack2.pop() else 0
44+
val sum = a + b + carry
45+
carry = sum / 10
46+
val node = ListNode(sum % 10)
47+
node.next = head
48+
head = node
49+
}
50+
return head
51+
}
52+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /