2

I am still learning Java, and currently working problems from Cracking the Coding Interview, and one of the problems on Chapter-2 (LinkedList) asks to remove duplicates from an unsorted linked List. I found a bunch of answers/solution on GitHub, but I would like to create my own Node, and write my own version.

What I have implemented so far is that I created Node class and write the function/method that can remove the duplicates from unsorted LinkedList, but when I try to test it, I tried to create the LinkedList in the main function, but I still have no idea how to figure it out. Can someone please help/guide me how to create a Singly LinkedList? Basically, I create four nodes (fourth,third,second,head), and connect them all using the Node class.

Thanks in advance,

public class Node {
    int data;
    Node next;

    public Node(int data, Node next){
        this.data = data;
        this.next = next;
    }

    public String toString(){
        return data + "";
    }
}


public class problem1 {

    public void Remove_duplicates(Node head){
        if(head == null){
            return;
        }

        Node current = head;
        while(current != null){
            Node runner = current;
            while(runner.next != null){
                if(runner.next.data == current.data){
                    runner.next = runner.next.next;
                }
                else {
                    runner = runner.next;
                }
            }
            current = current.next;
        }
    }

    public static void main(String[] args) {

        Node fourth = new Node(5,null);
        Node third = new Node(3,fourth);
        Node second = new Node(4,third);
        Node head = new Node(3,second);
        for(Node a: head){
            // ERROR: saying can only iterate over an array (or) java.lang.Iterable
            System.out.println(a.toString());
            a = a.next;
        }
    }
}
Steve
  • 1,553
  • 2
  • 20
  • 29
harrisonthu
  • 454
  • 3
  • 7
  • 18
  • `return data + "";` Blah. Try `return Integer.toString(data);` – bradimus Aug 15 '16 at 19:28
  • 2
    @bradimus the code `return data + "";` should work. – wake-0 Aug 15 '16 at 19:30
  • It will work, but "will work" and "good style" are not synonymous. Read through [this](http://stackoverflow.com/questions/3930210/java-int-to-string-integer-tostringi-vs-new-integeri-tostring) – bradimus Aug 15 '16 at 19:32
  • @bradimus it would also be possible to use `String.valueOf(data)`. so what is the better style? - I would suggest the second one because when you change from `int` to `double` it still works. – wake-0 Aug 15 '16 at 19:40
  • 2
    `String.valueOf(data‌​)` is equally good. `data + ""` does not express clear intent where as `String.valueOf(data‌​)` and `Integer.toString(dat‌​a)` do. Read through the question I liked. Some of the answers/comments address this as well as the efficiency of the statements. – bradimus Aug 15 '16 at 19:45
  • 1
    @bradimus Yep a good advice! - But next time it would be good when you add a reason in your comment why the code change should be done, so everyone would understand it :-) – wake-0 Aug 15 '16 at 19:51
  • Thanks guys for your comments, but I got it working since I was using foreach loop instead of while loop. – harrisonthu Aug 16 '16 at 00:32

1 Answers1

3

Try another kind of loop e.g. while

Node head = new Node(3, second);
Node node = head;
while (node.next != null) {
    System.out.println(node.toString());
    node = node.next;
}

Like it explains it does not know how to iterate over your nodes. Another approach for using the foreach would be to create an own class which implements the interface Iterable and does contain your LinkedList logic.

For the second approach I would suggest you to read the following: How can I implement the Iterable interface?

Community
  • 1
  • 1
wake-0
  • 3,918
  • 5
  • 28
  • 45