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.
2 parents 8e3cb71 + 3b5d417 commit 83ca70bCopy full SHA for 83ca70b
2181. Merge Nodes in Between Zeros
@@ -0,0 +1,21 @@
1
+class Solution {
2
+public:
3
+ ListNode* mergeNodes(ListNode* head){
4
+ //BASE CASE -> if we have a single zero, simply return null
5
+ if(!head->next) return nullptr;
6
+
7
+ //fetch sum from current 0 to next 0
8
+ ListNode* ptr= head->next;
9
+ int sum=0;
10
+ while(ptr->val!=0) sum+= ptr->val, ptr=ptr->next;
11
12
+ //assign sum on the first node between nodes having value 0.
13
+ head->next->val= sum;
14
15
+ //call and get the answer and connect the answer to next of head->next
16
+ head->next->next= mergeNodes(ptr);
17
18
+ //return head->next..=> new head
19
+ return head->next;
20
+}
21
+};
0 commit comments