-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQ2181MergeNodes.java
36 lines (29 loc) · 1.08 KB
/
Q2181MergeNodes.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
31
32
33
34
35
36
/*
@b-knd (jingru) on 01 Auguest 2022 10:59:00
*/
class Solution {
public ListNode mergeNodes(ListNode head) {
//initiate dummy nodes
ListNode dummyhead = new ListNode();
ListNode curr = head;
dummyhead.next = curr;
//use while loop to traverse the linked list
while(head != null){
int sum = 0;
//use while loop to traverse non-zero node and add up their sum
while(head.val != 0){
sum += head.val;
head = head.next;
}
//add sum of non-zeros values, create a new node, add to linked list and proceed to next node
if(sum > 0){
curr.next = new ListNode(sum);
curr = curr.next;
}
head = head.next;
}
return dummyhead.next.next;
}
}
//Runtime: 7 ms, faster than 94.27% of Java online submissions for Merge Nodes in Between Zeros.
//Memory Usage: 73 MB, less than 95.02% of Java online submissions for Merge Nodes in Between Zeros.