CJCoding With Joseph
15free / daySign inCreate account

A singly linked list can only be read from the front, so reaching a value counted from the end means working out its position first. A doubly linked list can start at tail and follow prev, which makes the last few values the cheapest ones to reach.

Requirements: 1. getFromEnd(int k) returns the value k steps before the last node, so k = 0 is the last value and k = 1 is the one before it. 2. It returns -1 when k is negative or is not less than size, which also covers an empty list. 3. Start at tail and follow prev. Do not walk from head.

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

40
30
10

= newline. The output ends with a newline, so print one after the last line.

Hints4 available
Topics
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.