Skip to content

added removeDuplicates() function #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
added removeDuplicates function
  • Loading branch information
Moetez Skouri authored and Moetez Skouri committed Apr 29, 2020
commit fc8e36c6e319c87cefa270c16e76c64416344588
13 changes: 13 additions & 0 deletions DataStructures/Lists/DoublyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,19 @@ else if (current == null)
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
Expand Down