Skip to content

Commit a43765d

Browse files
authored
Merge pull request TheAlgorithms#1292 from moetezz/moetezz
added removeDuplicates() function
2 parents cce363d + fc8e36c commit a43765d

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

DataStructures/Lists/DoublyLinkedList.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ else if (current == null)
160160
current.previous = newLink; // 1 <--> newLink <--> 2(current) <--> 3
161161
}
162162
}
163+
164+
public static void removeDuplicates(DoublyLinkedList l ) {
165+
Link linkOne = l.head ;
166+
while(linkOne.next != null) { // list is present
167+
Link linkTwo = linkOne.next; // second link for comparison
168+
while(linkTwo.next!= null) {
169+
if(linkOne.value == linkTwo.value) // if there are duplicates values then
170+
l.delete(linkTwo.value); // delete the link
171+
linkTwo = linkTwo.next ; // go to next link
172+
}
173+
linkOne = linkOne.next; // go to link link to iterate the whole list again
174+
}
175+
}
163176

164177
/**
165178
* Returns true if list is empty

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy