0

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.

1
  • This is not how you implement LinkedList. If it is home-work then i'm sure that you are asked to implement a linkedlist by yourself and not to use from Java's lib. Commented Jan 27, 2019 at 0:34

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.