Skip to content

Commit 83ca70b

Browse files
authored
Create 2181. Merge Nodes in Between Zeros (#521)
2 parents 8e3cb71 + 3b5d417 commit 83ca70b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

2181. Merge Nodes in Between Zeros

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)