From 3b5d4176fd756b0a359ac47e77ba677c944bb26b Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 4 Jul 2024 16:34:44 +0530 Subject: [PATCH] Create 2181. Merge Nodes in Between Zeros --- 2181. Merge Nodes in Between Zeros | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2181. Merge Nodes in Between Zeros diff --git a/2181. Merge Nodes in Between Zeros b/2181. Merge Nodes in Between Zeros new file mode 100644 index 0000000..abd4605 --- /dev/null +++ b/2181. Merge Nodes in Between Zeros @@ -0,0 +1,21 @@ +class Solution { +public: + ListNode* mergeNodes(ListNode* head){ + //BASE CASE -> if we have a single zero, simply return null + if(!head->next) return nullptr; + + //fetch sum from current 0 to next 0 + ListNode* ptr= head->next; + int sum=0; + while(ptr->val!=0) sum+= ptr->val, ptr=ptr->next; + + //assign sum on the first node between nodes having value 0. + head->next->val= sum; + + //call and get the answer and connect the answer to next of head->next + head->next->next= mergeNodes(ptr); + + //return head->next..=> new head + return head->next; +} +};