Skip to content

Commit fc8e36c

Browse files
Moetez SkouriMoetez Skouri
Moetez Skouri
authored and
Moetez Skouri
committed
added removeDuplicates function
1 parent 0389d31 commit fc8e36c

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

DataStructures/Lists/DoublyLinkedList.java

+13
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)