Skip to content

Commit e70397a

Browse files
Create 2181 Merge Nodes in Between Zeros.JAVA
1 parent 888686b commit e70397a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class Solution {
12+
public ListNode mergeNodes(ListNode head) {
13+
ListNode dummy = new ListNode();
14+
ListNode temp = dummy;
15+
ListNode curr = head.next;
16+
int sum = 0;
17+
while(curr != null){
18+
while(curr != null && curr.val != 0){
19+
sum = sum + curr.val;
20+
curr = curr.next;
21+
}
22+
if(curr != null){
23+
curr = curr.next;
24+
}
25+
temp.next = new ListNode(sum);
26+
temp = temp.next;
27+
sum = 0;
28+
}
29+
return dummy.next;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)