#include <bits/stdc++.h>
using
namespace
std;
struct
Node {
int
data;
Node* next;
};
Node* getNode(
int
data)
{
Node* newNode = (Node*)
malloc
(
sizeof
(Node));
newNode->data = data;
newNode->next = NULL;
return
newNode;
}
Node* segValidInvalidNum(Node* head)
{
Node *validStart = NULL, *validEnd = NULL;
Node *invalidStart = NULL, *invalidEnd = NULL;
Node* currentNode = head;
while
(currentNode != NULL) {
int
element = currentNode->data;
if
(element != 0) {
if
(validStart == NULL) {
validStart = currentNode;
validEnd = validStart;
}
else
{
validEnd->next = currentNode;
validEnd = validEnd->next;
}
}
else
{
if
(invalidStart == NULL) {
invalidStart = currentNode;
invalidEnd = invalidStart;
}
else
{
invalidEnd->next = currentNode;
invalidEnd = invalidEnd->next;
}
}
currentNode = currentNode->next;
}
if
(validStart == NULL || invalidStart == NULL)
return
head;
validEnd->next = invalidStart;
invalidEnd->next = NULL;
head = validStart;
return
head;
}
Node* modifyAndRearrangeList(Node* head)
{
if
(head == NULL || head->next == NULL)
return
head;
Node* ptr = head;
while
(ptr && ptr->next) {
if
((ptr->data == 0) || (ptr->data != ptr->next->data))
ptr = ptr->next;
else
{
ptr->data = 2 * ptr->data;
ptr->next->data = 0;
ptr = ptr->next->next;
}
}
return
segValidInvalidNum(head);
}
void
printList(Node* head)
{
while
(head != NULL) {
cout << head->data <<
" "
;
head = head->next;
}
}
int
main()
{
Node* head = getNode(2);
head->next = getNode(2);
head->next->next = getNode(0);
head->next->next->next = getNode(4);
head->next->next->next->next = getNode(0);
head->next->next->next->next->next = getNode(8);
cout <<
"Original List: "
;
printList(head);
head = modifyAndRearrangeList(head);
cout <<
"\nModified List: "
;
printList(head);
return
0;
}