Remove the Last Node of a Doubly Linked List
mediumQuestion 450In a singly linked list, removing the last node requires walking the chain to find the node before tail. In a doubly linked list, tail.prev gives you that node directly, so this removal is O(1) with no full walk.
Requirements:
1. removeLast() unlinks the last node and returns its value, or -1 on an empty list.
2. When only one node remains, head and tail both become null.
3. Otherwise move tail onto tail.prev and clear the next link of the new tail, so a backward walk still ends correctly. Keep size correct.
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
30 10 -> 20 -> null 20 -> 10 -> null 2
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...