forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2181.java
30 lines (27 loc) · 859 Bytes
/
_2181.java
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
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import java.util.ArrayList;
import java.util.List;
public class _2181 {
public static class Solution1 {
public ListNode mergeNodes(ListNode head) {
List<Integer> list = new ArrayList<>();
while (head != null) {
list.add(head.val);
head = head.next;
}
ListNode pre = new ListNode(-1);
ListNode tmp = pre;
for (int i = 1; i < list.size(); i++) {
int sum = 0;
while (i < list.size() && list.get(i) != 0) {
sum += list.get(i);
i++;
}
tmp.next = new ListNode(sum);
tmp = tmp.next;
}
return pre.next;
}
}
}