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 f10a450

Browse files
committed
feat: add leetcode question #25
1 parent 48e1fd2 commit f10a450

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.hi.dhl.algorithms.leetcode._002.java;
2+
3+
import com.hi.dhl.algorithms.model.ListNode;
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2022年5月12日
9+
* desc :
10+
* </pre>
11+
*/
12+
class Solution {
13+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
14+
int carry = 0;
15+
ListNode head = new ListNode(0);
16+
ListNode pre = head;
17+
while (l1 != null || l2 != null || carry > 0) {
18+
int a = l1 != null ? l1.val : 0;
19+
int b = l2 != null ? l2.val : 0;
20+
int sum = a + b + carry;
21+
carry = sum / 10;
22+
ListNode node = new ListNode(sum % 10);
23+
pre.next = node;
24+
pre = node;
25+
if (l1 != null) l1 = l1.next;
26+
if (l2 != null) l2 = l2.next;
27+
}
28+
return head.next;
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.hi.dhl.algorithms.leetcode._002.kotlin
2+
3+
import com.hi.dhl.algorithms.model.ListNode
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2022年5月12日
9+
* desc :
10+
* </pre>
11+
*/
12+
class Solution {
13+
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
14+
var p1 = l1
15+
var p2 = l2
16+
var carry = 0
17+
val head = ListNode(0)
18+
var pre = head
19+
while (p1 != null || p2 != null || carry != 0) {
20+
val a = p1?.`val` ?: 0
21+
val b = p2?.`val` ?: 0
22+
val sum = a + b + carry
23+
carry = sum / 10
24+
val node = ListNode(sum % 10)
25+
pre.next = node
26+
pre = node
27+
p1 = p1?.next
28+
p2 = p2?.next
29+
}
30+
return head.next
31+
}
32+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.hi.dhl.algorithms.leetcode._025.java;
2+
3+
import com.hi.dhl.algorithms.model.ListNode;
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2022年6月21日
9+
* desc :
10+
* </pre>
11+
*/
12+
class Solution {
13+
public ListNode reverseKGroup(ListNode head, int k) {
14+
int len = getListLength(head);
15+
ListNode dumpNode = new ListNode(0);
16+
dumpNode.next = head;
17+
ListNode p = dumpNode;
18+
ListNode q = dumpNode.next;
19+
int index = 0;
20+
while (q != null) {
21+
if (len - index < k) {
22+
break;
23+
}
24+
index = index + k;
25+
int m = k;
26+
while (--m != 0) {
27+
ListNode node = q.next;
28+
q.next = q.next.next;
29+
node.next = p.next;
30+
p.next = node;
31+
}
32+
p = q;
33+
q = q.next;
34+
}
35+
return dumpNode.next;
36+
}
37+
38+
private int getListLength(ListNode head) {
39+
ListNode cur = head;
40+
int len = 0;
41+
while (cur != null) {
42+
len++;
43+
cur = cur.next;
44+
}
45+
return len;
46+
}
47+
}

0 commit comments

Comments
(0)

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