Reputation: 15
I am just practicing my data structures and trying to make a method to delete duplicates from a singly linked list. This is what I have:
void removeDup() {
Node temp = head;
Node cur = null;
String s = "";
while(temp!=null) {
cur = temp;
if(!s.contains(temp.data + "")) {
s += temp.data + "";
}
else {
cur.next = temp.next;
}
temp = temp.next;
}
}
Printing the linked list after executing this method shows no changes. I believe this is because I am not properly linking the previous link to the current link's.next value, but everything looks correct to me. I debug it and it appears to delete the node correctly but the duplicate node still appears when I print out the linked list afterwards. Suggestions?
Upvotes: 0
Views: 231
Reputation: 119
The code is copied from https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/:
Method 1 - brute force,find all pairs of two nodes to see if they have same values, not sure if calling System.gc() is a good idea:
/* Function to remove duplicates from an
unsorted linked list */
void remove_duplicates() {
Node ptr1 = null, ptr2 = null, dup = null;
ptr1 = head;
/* Pick elements one by one */
while (ptr1 != null && ptr1.next != null) {
ptr2 = ptr1;
/* Compare the picked element with rest
of the elements */
while (ptr2.next != null) {
/* If duplicate then delete it */
if (ptr1.data == ptr2.next.data) {
/* sequence of steps is important here */
dup = ptr2.next;
ptr2.next = ptr2.next.next;
System.gc();
} else /* This is tricky */ {
ptr2 = ptr2.next;
}
}
ptr1 = ptr1.next;
}
}
Method 2 - using hashset to help detect duplicate, I personally like this method better:
/* Function to remove duplicates from a
unsorted linked list */
static void removeDuplicate(node head)
{
// Hash to store seen values, changed a little to compile for Java 8
HashSet<Integer> hs = new HashSet<Integer>();
/* Pick elements one by one */
node current = head;
node prev = null;
while (current != null)
{
int curval = current.val;
// If current value is seen before
if (hs.contains(curval)) {
prev.next = current.next;
} else {
hs.add(curval);
prev = current;
}
current = current.next;
}
}
Upvotes: 1
Reputation: 435
cur.next = temp.next
will not change anything. Use e.g. Java 8:
new LinkedList<>(Arrays.asList(1,2,1,3)).stream().distinct().collect(Collectors.toList());
or
new LinkedHashSet<>(new LinkedList<>(Arrays.asList(1,2,1,3)))
See also https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list
Upvotes: 0
Reputation: 1
First of all I think your choice of holding all the previous things in a single string is probably a bad idea.
For example, if you fed it a list with {x,y, xy}. the third item would be detected as a duplicate.
Couple simple alternative methods.
Hold previous values in some collection/ for each element check if anything else is equivalent.
sort everything, then check peoples neighbors.
you set cur = temp; at the top of your loop, so doing cur.next = temp.next; afterwards will do nothing. Don't set cur equal to temp at the top of your loop or just change it after.
Upvotes: 0