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 96f16d5

Browse files
committed
feat: add leetcode question #449
1 parent 7868ce8 commit 96f16d5

File tree

2 files changed

+116
-0
lines changed

2 files changed

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

0 commit comments

Comments
(0)

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