# List Reduction Hard

### PROBLEM STATEMENT`(`SIMPLIFIED`)`:

Given a linked list of `N` nodes such that each node have a lower case alphabet `(a - z)`. Your task is to remove the nodes which have the same data and are next to each other.

See original problem statement here

#### For Example:

``````Input : bddbcgdghgii

Output: cgdghg

Explanation : bddbcgdghgii -> bbcgdghg -> cgdghg``````

### SOLVING APPROACH:

1. The idea is to create another list `temp` to store the reduced version of the original list.

2. Traverse the original list and perform the following operations :-

1. If the `temp` list is empty, simply append the current element into the list.
2. If the `temp` list is not empty, check if the last element inserted is equal to the current element, If `Yes` remove the last element added.
3. Else if the last element is not equal to the current element, append the current element into the list. Finally our original list will be reduced and stored in the `temp` list.

#### ILLUSTRATION:

``````list = bddbcgdghgii
temp is empty

Start traversing the list :-

for 1st element b, temp is empty so append into it
temp = b

for 2nd element d, b is not equal to d so append into it
temp = bd

for 3rd element d, d is equal to d so remove already added d
temp = b

for 4th element b, b is equal to b so remove already added b, temp becomes empty now
temp =

for 5th element c, temp is empty so append into it
temp = c

for 6th element g, g is not equal to c so append into it
temp = cg

for 7th element d, d is not equal to g so append into it
temp = cgd

for 8th element g, g is not equal to d so append into it
temp = cgdg

for 9th element h, h is not equal to g so append into it
temp = cgdgh

for 10th element g, g is not equal to h so append into it
temp = cgdghg

for 11th element i, i is not equal to g so append into it
temp = cgdghgi

for 12th element i, i is equal to i so remove already added i
temp = cgdghg

So, our final reduced list is cgdghg``````

### SOLUTIONS:

```#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node {
char value;
struct Node *next;
}Node;

{
Node *newnode = (struct Node*)malloc(sizeof(struct Node));
newnode->value = val ;
newnode->next = NULL ;
if ( head == NULL ) {
}
else
{
}
}

if (temp) {
while ( temp!= NULL ) {
printf ( "%c", temp->value ) ;
temp = temp->next ;
}
}
}

{

while(t!=NULL)
{
char ch=t->value;

else {
free(temp);
}
else {
}
}
t=t->next;
}

}

int main()
{
struct  Node *head = NULL ;
int size, i;
char val;
scanf("%d", &size);
scanf(" %s", val);

for ( i = 0 ; i < size ; i ++ ) {
char ch=val[i];
}

else
printf("-1");

return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;
struct Node {
char data;
struct Node *next;
};

Node *insert( Node *head, char ch) {
Node *node = new Node();
}
node->data = ch;
return node;
}

{
return;
}
}

{

while(t!=NULL)
{
char ch=t->data;

else {
free(temp);
}
else {
}
}
t=t->next;
}

}

int main()
{
struct  Node *head = NULL, *temp ;
int size, i;
string val;
cin>>size;
cin>>val;
for ( i = 0 ; i < size ; i ++ ) {
char ch=val[i];
}
if(temp!=NULL)
PrintList(temp);
else
cout<<-1;
return 0;
}
```
```import java.io.*;
import java.util.*;
public class Main
{
char value;
this.value = nodeData;
this.next = null;
}
}

this.tail = null;
}

public void insertNode( char nodeData) {

this.tail=node;

} else {
}
}
}
{
while(temp!=null)
{
System.out.print(temp.value);
temp=temp.next;
}
}

{

while (current != null)
{
temp.insertNode(current.value);
} else {
}

current = current.next;
}
return temp;
}

private static Scanner scanner = new Scanner(System.in);
public static void main( String[] args) throws IOException {

int size = scanner.nextInt();
scanner.nextLine();
String val = scanner.next();

for (int i = 0; i < size; i++) {
char ch = val.charAt(i);

llist.insertNode(ch);
}
System.out.println("-1");
} else {
}
}
}
```
```class Node:
def __init__(self, data):
self.data = data
self.next = None

while t:
ch = t.data

else:
del temp
else:
t = t.next

new_node = Node(new_data)
new_node.data = new_data

def printList(node):
while (node != None):
print(node.data, end = " ")
node = node.next

if __name__=='__main__':

for i in "bddbcgdghgii":
Space Complexity: `O(N)`, for creating an `Additional linked list`.