Skip to content

Commit

Permalink
Create ZeroSumConsecutiveNodes.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Mar 12, 2024
1 parent b3c18e8 commit 056467f
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions Linked Lists/ZeroSumConsecutiveNodes.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
ListNode*front=new ListNode(0,head);

ListNode*start=front;
while(start!=NULL){
int presum=0;
ListNode*end=start->next;
while(end!=NULL){
presum+=end->val;
if(presum==0){
start->next=end->next;
}
end=end->next;
}
start=start->next;
}
return front->next;
}
};

0 comments on commit 056467f

Please sign in to comment.