We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 5866de4 commit 4fed490Copy full SHA for 4fed490
src/main/java/com/fishercoder/solutions/thirdthousand/_2181.java
@@ -27,4 +27,27 @@ public ListNode mergeNodes(ListNode head) {
27
return pre.next;
28
}
29
30
+
31
+ public static class Solution2 {
32
+ /**
33
+ * Without using an extra list, do sum on the fly.
34
+ */
35
+ public ListNode mergeNodes(ListNode head) {
36
+ ListNode pre = new ListNode(-1);
37
+ ListNode newHead = pre;
38
+ while (head != null && head.next != null) {
39
+ if (head.val == 0) {
40
+ int sum = 0;
41
+ head = head.next;
42
+ while (head.val != 0) {
43
+ sum += head.val;
44
45
+ }
46
+ newHead.next = new ListNode(sum);
47
+ newHead = newHead.next;
48
49
50
+ return pre.next;
51
52
53
0 commit comments