  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# List Reduction

Last Updated on November 28, 2022 by Prepbytes This blog Discusses the famous question “list reduction in linked list”. Linked list reduction plays an important role in improving your data structures like linked list. In linked list reduction, we have to remove the nodes. Criteria for removing nodes is the nodes with the same data and are next to each other will be removed from the linked list. Let’s discuss the reduction list in detail.

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 For Linked List Reduction:

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:

• If the `temp` list is empty, simply append the current element into the list.
• 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.
• 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 For Linked List Reduction:

``````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 For Linked List Reduction:

```#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 For Linked List Reduction: `O(N)`, for creating an `Additional linked list`.

This blog discusses the most efficient approach for the linked list reduction. Questions like linked list reduction always give a challenge for a programmer. Conquering linked list reduction will lead to one step moving towards your goals. To practice more problems on Linked list you can check out MYCODE | Competitive Programming.

## FAQ

1. How is data removed from a linked list?
To delete a node from the linked list, we need to do the following steps:

• Find the previous node of the node to be deleted.
• Change the next of the previous node.
• Free memory for the node to be deleted.

2. What are the 3 conditions in deleting a node in a linked list?
To delete a node from the linked list, we need to do the following steps.

• Find the previous node of the node to be deleted.
• Change the next to the previous node.
• Free memory for the node to be deleted.

3. Which companies recently asked linked list reduction questions in their technical interviews?