Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a Dummy Head?

So I want to know for sure what is a dummy head/dummy node in a linked list. Can somebody tell me the definition and give me an example maybe?

like image 273
Delia Pâncă Avatar asked May 19 '16 13:05

Delia Pâncă


People also ask

What is meant by dummy header?

dummy head in British English noun. a model of the human head with a microphone in each ear intended to receive sound in binaural and surround sound reproduction and transmission.

What is dummy head in Java?

The dummy head points to the answer list so we simply return the next node as the head of the answer. If we don't happen to find the item setting the dummy head to the original list makes it still point to the correct answer.

What are dummy head microphones used for?

The dummy head is designed to record multiple sounds at the same time enabling it to be exceptional at recording music as well as in other industries where multiple sound sources are involved.

What is ku100 used for?

The KU 100 is a dummy head microphone for a truly immersive binaural listening experience with headphones. Although it uses only two channels, its spatial depiction appears three dimensional and shockingly realistic. The KU 100 can be used to great effect in music and audio drama productions.


2 Answers

Dummy nodes are more like a hack and are commonly used when you want to avoid writing additional code for edge cases.

Consider the following case of inserting at tail in a linked list:

void insertAtTail(Node oldTail, int i){
    Node newTail = new Node(i);
    oldTail.next = newTail;
    return newTail;
}

This works fine when oldTail is not null. But imagine the scenario where we are trying to perform insertAtTail() on an empty list. The above written code will not work if the node is null. Therefore, we've to handle the edge case of checking if oldTail is null:

Node insertAtTail(Node oldTail, int i){
    Node newTail = new Node(i);
    if(oldTail == null) {return newTail;}
    oldTail.next = newTail;
    return newTail;
}

It's in scenarios like these that dummy nodes come in handy. Imagine I've a dummy node as follows:

Node dummy = new Node(0);

Now we pass this dummy node to the calling function:

insertAtTail(dummy, 5);

When a dummy node is passed to the calling function, you'll see that there's no need to check if dummy is null here. Therefore, we can skip the check for empty node:

Node insertAtTail(Node dummy, int i){
    Node newTail = new Node(i);
    dummy.next = newTail;
    return newTail;
}

As you can see, I've removed the check for null here.

like image 112
attaboy182 Avatar answered Sep 30 '22 19:09

attaboy182


When the head of the Linked List doesn't point to any Node, you create a Dummy Head (Node) pointed from that head. So that you would always be able to reach e.g. head.val or head.next without doing any extra null checks.

like image 44
InGeek Avatar answered Sep 30 '22 19:09

InGeek