-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMerge-Nodes-in-Between-Zeros.cpp
75 lines (67 loc) · 1.19 KB
/
Merge-Nodes-in-Between-Zeros.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <bits/stdc++.h>
using namespace std;
class ListNode
{
public:
int val;
ListNode *next;
ListNode(int nodeVal)
{
this->val = nodeVal;
next = NULL;
}
};
class Solution
{
public:
ListNode *mergeNodes(ListNode *head)
{
ListNode *prev = head, *temp = head->next;
while (temp)
{
if (temp->val != 0)
prev->val += temp->val;
else
{
if (temp->next)
{
prev->next = temp;
prev = temp;
}
else
prev->next = NULL;
}
temp = temp->next;
}
return head;
}
};
void printList(ListNode *head)
{
cout << head->val << " ";
if (head->next)
printList(head->next);
}
int main()
{
Solution st;
ListNode *head = new ListNode(0);
ListNode *n1 = new ListNode(3);
ListNode *n2 = new ListNode(1);
ListNode *n3 = new ListNode(0);
ListNode *n4 = new ListNode(4);
ListNode *n5 = new ListNode(5);
ListNode *n6 = new ListNode(2);
ListNode *n7 = new ListNode(0);
head->next = n1;
n1->next = n2;
n2->next = n3;
n3->next = n4;
n4->next = n5;
n5->next = n6;
n6->next = n7;
ListNode *res = st.mergeNodes(head);
printList(res);
cout << endl;
return 0;
}