Remove duplicates from linked list

Remove duplicates from an unsorted linked list

Write a removeDuplicates() function that 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.

Remove duplicates from a sorted linked list

Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.

C++


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

#include <iostream>

#include <unordered_set>

using namespace std;

// A Linked List Node

struct Node

{

int data;

Node* next;

};

// Helper function to print a given linked list

void printList(Node* head)

{

Node* ptr = head;

while (ptr)

{

cout << ptr->data << " —> ";

ptr = ptr->next;

}

cout << "nullptr";

}

// Helper function to insert a new node at the beginning of the linked list

void push(Node** headRef, int data)

{

Node* newNode = new Node();

newNode->data = data;

newNode->next = *headRef;

*headRef = newNode;

}

// Function to remove duplicates from a sorted list

void removeDuplicates(Node* head)

{

Node* previous = nullptr;

Node* current = head;

// take an empty set to store linked list nodes for future reference

unordered_set<int> set;

// do till the linked list is empty

while (current != nullptr)

{

// if the current node is seen before, ignore it

if (set.find(current->data) != set.end()) {

previous->next = current->next;

}

else {

// insert the current node into the set and proceed to the next node

set.insert(current->data);

previous = current;

}

current = previous->next;

}

}

int main()

{

// input keys

int keys[] = {5, 3, 4, 2, 5, 4, 1, 3};

int n = sizeof(keys)/sizeof(keys[0]);

// points to the head node of the linked list

Node* head = nullptr;

// construct a linked list

for (int i = n-1; i >= 0; i--) {

push(&head, keys[i]);

}

removeDuplicates(head);

// print linked list

printList(head);

return 0;

}

DownloadRun Code

Output:

5 —> 3 —> 4 —> 2 —> 1 —> nullptr

Program to remove duplicate elements from a singly linked list

Explanation

In this program, we need to remove the duplicate nodes from the given singly linked list.

Original List:

Remove duplicates from linked list

List after removing duplicate nodes:

Remove duplicates from linked list

In the above list, node 2 is repeated thrice, and node 1 is repeated twice. Node current will point to head, and index will point to node next to current. Start traversing the list till a duplicate is found that is when current's data is equal to index's data. In the above example, the first duplicate will be found at position 4. Assign current to another node temp. Connect temp's next node with index's next node. Delete index which was pointing to duplicate node. This process will continue until all duplicates are removed.

Algorithm

  1. Create a class Node which has two attributes: data and next. Next is a pointer to the next node in the list.
  2. Create another class RemoveDuplicate which has two attributes: head and tail.
  3. addNode() will add a new node to the list:
    1. Create a new node.
    2. It first checks, whether the head is equal to null which means the list is empty.
    3. If the list is empty, both head and tail will point to a newly added node.
    4. If the list is not empty, the new node will be added to end of the list such that tail's next will point to a newly added node. This new node will become the new tail of the list.
  4. removeDuplicate() will remove duplicate nodes from the list.
    1. Define a new node current which will initially point to head.
    2. Node temp will point to current and index will always point to node next to current.
    3. Loop through the list till current points to null.
    4. Check whether current?s data is equal to index's data that means index is duplicate of current.
    5. Since index points to duplicate node so skip it by making node next to temp to will point to node next to index, i.e. temp.next = index.next.
  5. display() will display the nodes present in the list:
    1. Define a node current which will initially point to the head of the list.
    2. Traverse through the list till current points to null.
    3. Display each node by making current to point to node next to it in each iteration.

Solution

Python

Output:

Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4

C

Output:

Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4

JAVA

Output:

Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4

C#

Output:

Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4

PHP

Output:

Originals list: 1 2 3 2 2 4 1 List after removing duplicates: 1 2 3 4