Remove duplicates from an unsorted linked list

原文链接:link
Write a removeDuplicates() function which takes a list and deletes any duplicate nodes from the list. The list is not sorted.
For example if the linked list is 12->11->12->21->41->43->21 then removeDuplicates() should convert the list to 12->11->21->41->43.

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

METHOD 1 (Using two loops)

This is the simple way where two loops are used. Outer loop is used to pick the elements one by one and inner loop compares the picked element with rest of the elements.

Thanks to Gaurav Saxena for his help in writing this code.

/* Program to remove duplicates in an unsorted 
linked list */
#include<bits/stdc++.h> 
using namespace std; 

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

// Utility function to create a new Node 
struct Node *newNode(int data) 
{ 
Node *temp = new Node; 
temp->data = data; 
temp->next = NULL; 
return temp; 
} 

/* Function to remove duplicates from a 
unsorted linked list */
void removeDuplicates(struct Node *start) 
{ 
	struct Node *ptr1, *ptr2, *dup; 
	ptr1 = start; 

	/* Pick elements one by one */
	while (ptr1 != NULL && ptr1->next != NULL) 
	{ 
		ptr2 = ptr1; 

		/* Compare the picked element with rest 
		of the elements */
		while (ptr2->next != NULL) 
		{ 
			/* If duplicate then delete it */
			if (ptr1->data == ptr2->next->data) 
			{ 
				/* sequence of steps is important here */
				dup = ptr2->next; 
				ptr2->next = ptr2->next->next; 
				delete(dup); 
			} 
			else /* This is tricky */
				ptr2 = ptr2->next; 
		} 
		ptr1 = ptr1->next; 
	} 
} 

/* Function to print nodes in a given linked list */
void printList(struct Node *node) 
{ 
	while (node != NULL) 
	{ 
		printf("%d ", node->data); 
		node = node->next; 
	} 
} 

/* Druver program to test above function */
int main() 
{ 
	/* The constructed linked list is: 
	10->12->11->11->12->11->10*/
	struct Node *start = newNode(10); 
	start->next = newNode(12); 
	start->next->next = newNode(11); 
	start->next->next->next = newNode(11); 
	start->next->next->next->next = newNode(12); 
	start->next->next->next->next->next = 
									newNode(11); 
	start->next->next->next->next->next->next = 
									newNode(10); 

	printf("Linked list before removing duplicates "); 
	printList(start); 

	removeDuplicates(start); 

	printf("\nLinked list after removing duplicates "); 
	printList(start); 

	return 0; 
}

Output :

Linked list before removing duplicates:
 10 12 11 11 12 11 10 
Linked list after removing duplicates:
 10 12 11 
Time Complexity: O(n^2)

METHOD 2 (Use Sorting)

In general, Merge Sort is the best-suited sorting algorithm for sorting linked lists efficiently.

  1. Sort the elements using Merge Sort. We will soon be writing a post about sorting a linked list. O(nLogn)
  2. Remove duplicates in linear time using the algorithm for removing duplicates in sorted Linked List. O(n)

Please note that this method doesn’t preserve the original order of elements.

Time Complexity: O(nLogn)

METHOD 3 (Use Hashing)

We traverse the link list from head to end. For every newly encountered element, we check whether it is in the hash table: if yes, we remove it; otherwise we put it in the hash table.

/* Program to remove duplicates in an unsorted 
linked list */
#include<bits/stdc++.h> 
using namespace std; 

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

// Utility function to create a new Node 
struct Node *newNode(int data) 
{ 
Node *temp = new Node; 
temp->data = data; 
temp->next = NULL; 
return temp; 
} 

/* Function to remove duplicates from a 
unsorted linked list */
void removeDuplicates(struct Node *start) 
{ 
	// Hash to store seen values 
	unordered_set<int> seen; 

	/* Pick elements one by one */
	struct Node *curr = start; 
	struct Node *prev = NULL; 
	while (curr != NULL) 
	{ 
		// If current value is seen before 
		if (seen.find(curr->data) != seen.end()) 
		{ 
		prev->next = curr->next; 
		delete (curr); 
		} 
		else
		{ 
		seen.insert(curr->data); 
		prev = curr; 
		} 
		curr = prev->next; 
	} 
} 

/* Function to print nodes in a given linked list */
void printList(struct Node *node) 
{ 
	while (node != NULL) 
	{ 
		printf("%d ", node->data); 
		node = node->next; 
	} 
} 

/* Driver program to test above function */
int main() 
{ 
	/* The constructed linked list is: 
	10->12->11->11->12->11->10*/
	struct Node *start = newNode(10); 
	start->next = newNode(12); 
	start->next->next = newNode(11); 
	start->next->next->next = newNode(11); 
	start->next->next->next->next = newNode(12); 
	start->next->next->next->next->next = 
									newNode(11); 
	start->next->next->next->next->next->next = 
									newNode(10); 

	printf("Linked list before removing duplicates : \n"); 
	printList(start); 

	removeDuplicates(start); 

	printf("\nLinked list after removing duplicates : \n"); 
	printList(start); 

	return 0; 
} 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值