Add to the Back of a Doubly Linked List
easyQuestion 560Because the list keeps a tail reference, adding at the back needs no walk at all: it is O(1), the same cost as adding at the front. The new node must be linked in both directions, forward from the old tail and back to it. addFirst is already written.
Requirements:
1. addLast(int value) puts a new node at the back of the list without walking the chain.
2. On an empty list the new node becomes both head and tail.
3. Otherwise the old tail points forward at the new node, the new node points back at the old tail, and tail moves onto the new node.
4. size goes up by one in both cases.
The DNode class is provided. Do not rename it and do not add public to it, because a Java source file may contain only one public class.
Do NOT write a main method.
Expected output
10 -> 20 -> 30 -> null 30 -> 20 -> 10 -> null 3
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...