Java: how to use dummy node or mark a node as dummy node

3.4k views Asked by At

A question asking you to delete the middle node in a linked list, only that node is give. The way to solve the problem is copy middle.next.element to middle.element and then delete middle.next by doing middle.next=middle.next.next

There's a special case which is when middle.next is the last node. The answer say that you could mark the middle node as dummy.

I'm not familiar with the idea "dummy". How to mark a node as dummy and how to use a dummy node in other cases?

What about dummy data in general?

1

There are 1 answers

0
Marko Topolnik On BEST ANSWER

There is no general answer to this question. The implementation of the linked list must already define the concept of a dummy node and use it consistently. A typical way to achieve this would be by declaring a special dummy instance:

public class Node {
   public static final Object DUMMY = new Object();
   ...

and assigning middle.element = DUMMY;

As you can imagine, this will have no effect unless all the rest of the API implementation abides by this convention.

If your list is specified as unable to contain null elements, then you could also assign middle.element = null, but the rest of the story stays the same.