<p>Given the <code>head</code> of a linked list, we repeatedly delete consecutive sequences of nodes that sum to <code>0</code> until there are no such sequences.</p>

<p>After doing so, return the head of the final linked list.&nbsp; You may return any such answer.</p>

<p>&nbsp;</p>
<p>(Note that in the examples below, all sequences are serializations of <code>ListNode</code> objects.)</p>

<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,-3,3,1]
<strong>Output:</strong> [3,1]
<strong>Note:</strong> The answer [1,2,1] would also be accepted.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,3,-3,4]
<strong>Output:</strong> [1,2,4]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> head = [1,2,3,-3,-2]
<strong>Output:</strong> [1]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The given linked list will contain between <code>1</code> and <code>1000</code> nodes.</li>
	<li>Each node in the linked list has <code>-1000 &lt;= node.val &lt;= 1000</code>.</li>
</ul>

/**
 * 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) {
        struct ListNode *temp=head;
        struct ListNode *newhead=0;
        if(temp==0)
        {
            newhead=0;
        }
        else if(temp->next==0 && temp->val!=0)
        {
            newhead=temp;
        }
        else if(temp->next==0 && temp->val==0)
        {
            newhead=0;
        }
        else
        {
            temp=head;
            vector<int> arr;
            while(temp!=0)
            {
                arr.push_back(temp->val);
                temp=temp->next;
            }
            int i=0,j;
            int flag=0;
            vector<int> nums;
            int sum=0,start,end;
            while(i<arr.size())
            {
                flag=0;
                sum=0;
                for(j=i;j<arr.size();j++)
                {
                    sum=sum+arr[j];
                    if(sum==0)
                    {
                        start=i;
                        end=j;
                        flag=1;
                        break;
                    }
                }
                if(flag==1)
                {
                    nums.clear();
                    for(i=0;i<arr.size();i++)
                    {
                        if(i<start || i>end)
                        {
                            nums.push_back(arr[i]);
                        }
                    }
                    arr.clear();
                    for(i=0;i<nums.size();i++)
                    {
                        arr.push_back(nums[i]);
                    }
                    i=0;
                }
                else
                {
                    i=i+1;
                }
            }
            struct ListNode *newnode;
            for(i=0;i<arr.size();i++)
            {
                newnode=new ListNode();
                newnode->val=arr[i];
                newnode->next=0;
                if(newhead==0)
                {
                    newhead=newnode;
                    temp=newnode;
                }
                else
                {
                    temp->next=newnode;
                    temp=newnode;
                }
            }

        }
        return newhead;
    }
};
15300