-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2181-Merge_Nodes_in_Between_Zeros.cpp
More file actions
181 lines (165 loc) · 3.93 KB
/
2181-Merge_Nodes_in_Between_Zeros.cpp
File metadata and controls
181 lines (165 loc) · 3.93 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*******************************************************************************
* 2181-Merge_Nodes_in_Between_Zeros.cpp
* Billy.Ljm
* 04 July 2024
*
* =======
* Problem
* =======
* https://leetcode.com/problems/merge-nodes-in-between-zeros/
*
* You are given the head of a linked list, which contains a series of integers
* separated by 0's. The beginning and end of the linked list will have
* Node.val == 0.
*
* For every two consecutive 0's, merge all the nodes lying in between them into
* a single node whose value is the sum of all the merged nodes. The modified
* list should not contain any 0's.
*
* Return the head of the modified linked list.
*
* ===========
* My Approach
* ===========
* We'll use two pointers. One crawls to the next zero, summing the nodes it
* passes. The other stays at the previous zero so that we can connect it to the
* new merged node. I'll make sure to delete the nodes as I merge them.
*
* This has a time complexity of O(n) and space complexity of O(1), where n is
* the length of the array.
******************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* 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) {}
};
/**
* Constructs singly-linked list from vector
*/
ListNode* LinkedList(const vector<int>& values) {
if (values.empty()) return nullptr; // trivial case
// construct linked list
ListNode* head = new ListNode(values[0]);
ListNode* crawler = head;
for (size_t i = 1; i < values.size(); ++i) {
crawler->next = new ListNode(values[i]);
crawler = crawler->next;
}
return head;
}
/**
* Deletes a linked list and all nodes
*/
void DeleteLinkedList(ListNode* head) {
ListNode* next;
while (head != nullptr) {
next = head->next;
delete head;
head = next;
}
}
/**
* Prints a LinkedList as a vector
*/
std::ostream& operator<<(std::ostream& os, ListNode* head) {
os << "[";
ListNode* crawler = head;
while (crawler != nullptr) {
os << crawler->val << ",";
crawler = crawler->next;
}
if (head != nullptr) os << "\b";
os << "]";
return os;
}
/**
* Solution
*/
class Solution {
private:
ListNode* crawl(ListNode* crawler) {
ListNode* next = crawler->next;
delete crawler;
crawler = next;
return crawler;
}
public:
ListNode* mergeNodes(ListNode* head) {
int summ = 0;
ListNode *crawler, * prev;
// ignore any initial zeros
while (head->val == 0) {
head = crawl(head);
}
// merge first nodes to new head
summ = 0;
while (head->val != 0) {
summ += head->val;
head = crawl(head);
}
head->val = summ;
// merge rest of list
crawler = head;
while (crawler != nullptr) {
prev = crawler;
crawler = crawler->next;
summ = 0;
while (crawler != nullptr and crawler->val != 0) {
summ += crawler->val;
crawler = crawl(crawler);
}
if (crawler != nullptr) {
if (summ > 0) {
crawler->val = summ;
prev->next = crawler;
}
}
else { // at end of list
if (summ > 0) prev->next = new ListNode(summ);
else prev->next = nullptr;
}
}
return head;
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
ListNode* head;
// test case 1
head = LinkedList({ 0,3,1,0,4,5,2,0 });
std::cout << "mergeNodes(" << head << ") = ";
head = sol.mergeNodes(head);
std::cout << head << std::endl;
DeleteLinkedList(head);
// test case 2
head = LinkedList({ 0,1,0,3,0,2,2,0 });
std::cout << "mergeNodes(" << head << ") = ";
head = sol.mergeNodes(head);
std::cout << head << std::endl;
DeleteLinkedList(head);
return 0;
}