From fc8e36c6e319c87cefa270c16e76c64416344588 Mon Sep 17 00:00:00 2001 From: Moetez Skouri Date: Wed, 29 Apr 2020 19:07:09 +0100 Subject: [PATCH] added removeDuplicates function --- DataStructures/Lists/DoublyLinkedList.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/DataStructures/Lists/DoublyLinkedList.java b/DataStructures/Lists/DoublyLinkedList.java index 677487e1d..e1b0041dd 100644 --- a/DataStructures/Lists/DoublyLinkedList.java +++ b/DataStructures/Lists/DoublyLinkedList.java @@ -160,6 +160,19 @@ public class DoublyLinkedList { current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3 } } + + public static void removeDuplicates(DoublyLinkedList l ) { + Link linkOne = l.head ; + while(linkOne.next != null) { // list is present + Link linkTwo = linkOne.next; // second link for comparison + while(linkTwo.next!= null) { + if(linkOne.value == linkTwo.value) // if there are duplicates values then + l.delete(linkTwo.value); // delete the link + linkTwo = linkTwo.next ; // go to next link + } + linkOne = linkOne.next; // go to link link to iterate the whole list again + } + } /** * Returns true if list is empty