johnjohnson2755
johnjohnson2755

Reputation: 1

Why is my code not deleting the duplicate nodes? My output is still 12311

I am trying to delete duplicate nodes in an unsorted linked list. However, my output is not changing the linked list at all.

I tried to replace this code:

            list.add(new Node(1));
    list.add(new Node(2));
    list.add(new Node(3));
    list.add(new Node(1));
    list.add(new Node(1));

with

            Node firstNode = new Node(1);
            Node secondNode = new Node(2);
            Node thirdNode = new Node(3);
            Node fourthNode = new Node(1);
            Node fifthNode = new Node(1);

            firstNode.next = secondNode;
            secondNode.next = thirdNode;
            thirdNode.next = fourthNode;
            fourthNode.next = fifthNode;

            list.add(firstNode);
    list.add(secondNode);
    list.add(thirdNode);
    list.add(fourthNode);
    list.add(fifthNode);

but still no luck.

import java.util.HashSet;
import java.util.LinkedList;

class Node {

  public int value;
  public Node next;

  public Node(int value) {
    this.value = value;
  }  
}

class LinkedLists {

  Node head;

  public static void removeDups(Node n) {

    HashSet<Integer> set = new HashSet<Integer>();
    Node prev = null;

    if (n == null) {
        return;
    }

    while(n != null) {

        if (set.contains(n.value)) {
            prev.next = n.next;
        } else {
            set.add(n.value);
            prev = n;
        }
        n = n.next;
    }           
}

public static void main(String[] args) {

    LinkedList<Node> list = new LinkedList<Node>();

    list.add(new Node(1));
    list.add(new Node(2));
    list.add(new Node(3));
    list.add(new Node(1));
    list.add(new Node(1));

    removeDups(list.getFirst());

    for (int i = 0; i < list.size(); ++i) {
        System.out.print(list.get(i).value);
    }

}

}

I expect an output of 123.

Upvotes: 0

Views: 57

Answers (1)

jbx
jbx

Reputation: 22168

You are probably confused between implementing the Linked List yourself and using Java's own LinkedList collection.

Java's java.util.LinkedList manages the next and previous pointers internally. It does not allow you to manipulate them, or expect you to manage them. It implements a doubly-linked list, but that is abstracted from you.

You are adding 5 nodes to this LinkedList, and nowhere are you removing the duplicates.

Your Node class in this case is useless. You are updating a separate linked chain of next pointers, but you are not traversing it or using this in any way.

You could have simply added the numbers directly to the LinkedList, and removed the duplicates afterwards:

List<Integer> list = new LinkedList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(1);
list.add(1);

List<Integer> uniqueList = list.stream().distinct().collect(Collectors.toList());

for (int i = 0; i < uniqueList.size(); ++i) {
    System.out.print(list.get(i).value);
}

Upvotes: 1

Related Questions