﻿ C Program: Remove duplicates from a unsorted singly linked list

# C Exercises: Remove duplicates from a unsorted singly linked list

## C Singly Linked List : Exercise-16 with Solution

Write a C program to remove duplicates from a single unsorted linked list.

Sample Solution:

C Code:

``````#include<stdio.h>
#include <stdlib.h>

struct Node {
int data;
struct Node* next;
};

// Function to create a new node
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}

struct Node *current = head, *next_next;

// Check each node of the linked list
while (current->next != NULL) {
// Check if the current node's value is equal to the next node's value
if (current->data == current->next->data) {
next_next = current->next->next;
free(current->next);
current->next = next_next;
} else {
current = current->next;
}
}
}

// Function to print a linked list
}
printf("\n");
}

int main() {
printf("Original Singly List:\n");
printf("After removing duplicate elements from the said singly list:\n");
printf("\nOriginal Singly List:\n");
printf("After removing duplicate elements from the said singly list:\n");
return 0;
}
```
```

Sample Output:

```Original Singly List:
1 2 3 3 4
After removing duplicate elements from the said singly list:
1 2 3 4

Original Singly List:
1 2 3 3 4 4
After removing duplicate elements from the said singly list:
1 2 3 4
```

Flowchart :

C Programming Code Editor:

What is the difficulty level of this exercise?

﻿