Remove Duplicates From Linked List In Place - Java This is my implementation

class Ch02LinkedLists {
    void removeDuplicatesInPlace(Node head) {
        Node anchor = head;
        Node curr;

        while ((curr = anchor) != null) {
            while (curr.next != null)
                if (curr.next.val == anchor.val)
                    curr.next = curr.next.next;
                else
                    curr = curr.next;
            anchor = anchor.next;
        }
    }
}

class Node {
    char val;
    Node next;

    Node(char val) {
        this.val = val;
    }
}


And my test code

public class Ch02LinkedListsTest {

    Ch02LinkedLists linkedLists = new Ch02LinkedLists();

    @Test(timeout = 200) // Timeout to detect infinite loop
    public void removeDuplicatesInPlace() {
        Node a = new Node('a');
        Node a_dup = new Node('a');
        Node b = new Node('b');
        Node c = new Node('c');
        Node b_dup = new Node('b');
        Node b_dup_dup = new Node('b');
        Node c_dup = new Node('c');
        Node c_dup_dup = new Node('c');

        a.next = a_dup;
        a_dup.next = b;
        b.next = c;
        c.next = b_dup;
        b_dup.next = b_dup_dup;
        b_dup_dup.next = c_dup;
        c_dup.next = c_dup_dup;

        // a -&gt; a -&gt; b -&gt; c -&gt; b -&gt; b -&gt; c -&gt; c
        linkedLists.removeDuplicatesInPlace(a);
        // a -&gt; b -&gt; c

        assertThat(a.val, equalTo('a'));
        assertThat(a.next.val, equalTo('b'));
        assertThat(a.next.next.val, equalTo('c'));
        assertThat(a.next.next.next, equalTo(null));
    }

}


As you see I have a single statement in my inner while hence no need for the curly braces..

while (curr.next != null)
    if (curr.next.val == anchor.val)
        curr.next = curr.next.next;
    else
        curr = curr.next;


Is there any way to achieve it for the outer loop, i.e. for:

while ((curr = anchor) != null) {
    // inner while here
    anchor = anchor.next;
}


so that I can remove the curly braces from the outer while as well?

Also, any other review is welcome.
Classes not designed for extension should be made final. Variables which should not change after initial assignment should also be made final. In addition to documenting design intent, they make it easier to read the code because you know they won't change.

As @IEatBagels said, removing curly braces is not a reasonable design goal. Code readability should be maximized. In particular, even optional curly braces are desirable in almost all cases, both because they enhance readability and because they avoid an obnoxious bug when you need to add a second statement and forget to add the curly braces.

In idiomatic java, underscores are only used in the names of constants. Prefer the use of camelCase.

For readability, it's preferable to use full, descriptive variable names rather than abbreviations.

In non-trivial code, you'd often want to encapsulate the variables in Node. This lets you change the types of those variables without breaking the contract of Node.

If you build your test list slightly differently, you don't need all those new variables. Even better would be to use a method to create the list for you.

A toString implementation on Node might be nice. Then you can just print the node directly, rather than asking for the value.

An arguably superior approach would use a HashSet&lt;Character&gt; rather than nested iterations of the list. You cut the runtime significantly at the cost of some memory. If you're not allowed to use a HashSet, an array of size 26 mapped to the letters of the alphabet would also work (assuming the input is constrained to all lowercase letters).

If you made all these changes, your code might look more like:

void removeDuplicatesInPlace(final Node head) {
    final Set&lt;Character&gt; listValues = new HashSet&lt;&gt;();
    Node current = head;
    listValues.add(current.val);

    while (current.next != null) {
        if (!listValues.add(current.next.val)) {
            current.next = current.next.next;
        } else {
            current = current.next;
        }
    }
}




class Node {
    final char val;
    Node next;

    Node(char val) {
        this.val = val;
    }

    @Override
    public String toString() {
        return Character.toString(val);
    }
}




public void removeDuplicatesInPlace() {
    final Node head = new Node('a');
    Node current = head;

    current.next = new Node('a');
    current = current.next;

    current.next = new Node('b');
    current = current.next;

    current.next = new Node('c');
    current = current.next;

    current.next = new Node('b');
    current = current.next;

    current.next = new Node('b');
    current = current.next;

    current.next = new Node('c');
    current = current.next;

    current.next = new Node('c');
    current = current.next;

    // a -&gt; a -&gt; b -&gt; c -&gt; b -&gt; b -&gt; c -&gt; c
    this.removeDuplicatesInPlace(head);
    // a -&gt; b -&gt; c

    current = head;
    while (current != null) {
        System.out.print(current + " -&gt; ");
        current = current.next;
    }
}




private static Node buildList(final char... values) {
    final Node head = new Node(values[0]);
    Node current = head;

    for (int i = 1; i &lt; values.length; i++) {
        current.next = new Node(values[i]);
        current = current.next;
    }

    return head;
}