Home » C++ programs

Eliminate duplicates from Linked List using C++ program

Here, we are going to learn how to eliminate duplicated from the linked list using a C++ program?
Submitted by Indrajeet Das, on December 19, 2018

Given a sorted linked list (elements are sorted in ascending order). Eliminate duplicates from the given LL, such that output LL contains only unique elements.

Input format: Linked list elements (separated by space and terminated by 1)

    Sample Input 1 :
    1 2 3 3 3 4 4 5 5 5 7 -1

    Sample Output 1 :
    1 2 3 4 5 7

Description:

In this question, we are given a sorted linked list with duplicate elements in it. Our task is to remove the duplicate nodes of the linked list. Since the list is sorted, the duplicate elements will be present in consecutive orders, that makes the work a lot easier.

Example:

    Lets the list be:
    1->2->3->3->4->4->4->NULL
    The modified list will be:
    1->2->3->4->NULL

Solution Explanation:

To solve this problem, we keep the temp pointer pointing to node. While(temp -> next != NULL), we check if the data of temp is equal to data of temp->next. If they are equal, we will point the temp-> next to temp -> next ->next.
Original LL: 3-> 3-> 4-> ...
After Change: 3-> 4-> ... Leaving out the middle 3->

This will keep on until we reached the end of the list and return head at the end.

Steps

    1.	1->2->3->3->4->4->4->NULL, temp = 1
    2.	1->2->3->3->4->4->4->NULL, temp = 2
    3.	1->2->3->3->4->4->4->NULL, temp = 3
    4.	1->2->3->4->4->4->NULL, temp = 4
    5.	1->2->3->4->4->NULL, temp = 4
    Finally,
    1->2->3->4->NULL

Function:

Node *removeDuplicate(Node* head){
	//temp pointing to head
	Node *temp = head;            
	while(temp->next != NULL && temp != NULL){
		//Duplicate Found
		if(temp->data == temp->next->data){      
			//DUplicate Removed
			temp -> next = temp ->next ->next;    
		}
		else{
			//No Duplicate Present
			temp = temp->next;               
		}
	}
	//Return Head
	return head;                
}

C++ Code:

#include<bits/stdc++.h>
using namespace std;

struct Node{// linked list Node
	int data;
	Node * next;
};

Node *newNode(int k){ //defining new node
	Node *temp = (Node*)malloc(sizeof(Node)); 
	temp->data = k; 
	temp->next = NULL; 
	return temp; 
}

//Used to add new node at the end of the list
Node *addNode(Node* head, int k){
	if(head == NULL){
		head = newNode(k);
	}
	else{
		Node * temp = head;
		Node * node = newNode(k);
		while(temp->next!= NULL){
			temp = temp->next;
		}
		temp-> next = node;
	}

	return head;
}

// Used to create new linked list and return head
Node *createNewLL(){
	int cont = 1;
	int data;
	Node* head = NULL;
	while(cont){
		cout<<"Enter the data of the Node"<<endl;
		cin>>data;
		head = addNode(head,data);
		cout<<"Do you want to continue?(0/1)"<<endl;
		cin>>cont;
	}
	return head;
}

//To print the Linked List
void *printLL(Node * head){
	while(head!= NULL){
		cout<<head->data<<"->";
		head = head-> next;
	}
	cout<<"NULL"<<endl;
}

//Function 
Node *removeDuplicate(Node* head){
	//temp pointing to head
	Node *temp = head;
	while(temp->next != NULL && temp != NULL){
		//Duplicate Found
		if(temp->data == temp->next->data){
			//DUplicate Removed
			temp -> next = temp ->next ->next;
		}
		else{
			//No Duplicate Present
			temp = temp->next;
		}
	}
	//Return Head
	return head;
}

//Driver Main
int main(){
	Node * head = createNewLL();
	cout<<"The linked list is"<<endl;
	printLL(head);

	head = removeDuplicate(head);
	cout<<"The new Linked List is" <<endl;
	printLL(head);

	return 0;
}

Output

Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
1
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
2
Do you want to continue?(0/1)
1
Enter the data of the Node
3
Do you want to continue?(0/1)
1
Enter the data of the Node
4
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
1
Enter the data of the Node
5
Do you want to continue?(0/1)
0
The linked list is
1->1->2->2->3->4->5->5->5->NULL
The new Linked List is
1->2->3->4->5->NULL





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.