-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_node.cpp
49 lines (44 loc) · 1.25 KB
/
merge_node.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//program to merge node from a singly linked list
//problem link: https://leetcode.com/problems/merge-nodes-in-between-zeros/
/**
* 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:
void insertAtTail(ListNode*&head,ListNode*&tail,int val)
{
ListNode*newNode=new ListNode(val);
if(head==NULL){
head=newNode;tail=newNode;return;
}
tail->next=newNode;tail=newNode;
}
ListNode* mergeNodes(ListNode* head) {
if(head==NULL)return head;
else if(head->val==0&&head->next==NULL)return NULL;
else if(head->val==0&&head->next->val==0&&head->next==NULL)return NULL;
ListNode*head2=NULL,*tail2=NULL;
ListNode*tmp=head;
int val=0;
while(tmp->next!=NULL)
{
if(tmp->next->val!=0)
{
val+=tmp->next->val;
}
else{
insertAtTail(head2,tail2,val);
val=0;
}
tmp=tmp->next;
}
return head2;
}
};