-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeNodesVal0.cpp
77 lines (69 loc) · 1.85 KB
/
MergeNodesVal0.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
76
77
#include<bits/stdc++.h>
using namespace std;
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) {}
};
// * Approach - 1: By creating a NEW LIST
ListNode* mergeNodes(ListNode* head) {
ListNode *ptr = head;
ListNode* newHead = NULL, *temp = newHead;
while(ptr){
int sum = 0;
while(ptr && ptr->val != 0){
sum += ptr->val;
ptr = ptr->next;
}
if(sum){
if(newHead == NULL){
newHead = new ListNode(sum);
temp = newHead;
}
else{
temp->next = new ListNode(sum);
temp = temp->next;
}
}
ptr = ptr->next;
}
return newHead;
}
// * Approach-2: Merginng the nodes, w/o creating a new node.
ListNode* mergeNodes(ListNode* head) {
ListNode *currPtr = head, *prevPtr = NULL;
// * Edge case (not neccessary)
// * As the question specifiecs, there's no such TC with consecutive 0s
if(!head || head->next == 0)
return head;
// * Initial configuration: Find the 1st non-zero node
currPtr = head->next;
delete head;
head = currPtr;
// * Initial Config end
while(currPtr) {
int sum = 0;
// * Add all the non-zero nodes, and delete during traversal
while(currPtr && currPtr->val != 0){
sum += currPtr->val;
ListNode* temp = currPtr;
currPtr = currPtr->next;
delete temp;
}
// * Update current node's value with sum
currPtr->val = sum;
// * If this is a 2nd 0
if(prevPtr == NULL){
prevPtr = currPtr;
head = prevPtr;
} else {
prevPtr->next = currPtr;
prevPtr = currPtr;
}
currPtr = currPtr->next;
prevPtr->next = NULL;
}
return head;
}