← Back to Data Structures & Algorithms
Question 450
Remove the Last Node of a Doubly Linked List
Remove the Last Node of a Doubly Linked List
In a singly linked list, removing the last node requires walking the chain to find the node before tail. In a doubly linked list,tail.prevgives 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 ontotail.prevand 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 — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Data Structures & AlgorithmsLinked Lists
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (sysout, psvm, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.