I've got the following class
class ListNode
{
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
My task is to write a method that:
- Takes
int[]as a parameter. - Reverses the array.
- Returns
ListNodewhich has value of the current element in the array, has.nextof a new created ListNode with the corresponding value in the array and so on.
Given the input of new int[]{0, 8, 7}; I expect to get a ListNode where value is 7, .next is a new node with the value of 8 and finally .next.next is a new node with the value of 0.
I wrote the following methods to accomplish such behaviour
public static ListNode reverseAndLinkToListNode(int [] arrayToReverse)
{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
int finalDigitsArrayLength = arrayToReverse.length;
ListNode finalNode = new ListNode();
if (finalDigitsArrayLength > 0)
{
finalNode.val = arrayToReverse[finalDigitsArrayLength - 1];
int j = 1;
for (int i = finalDigitsArrayLength - 2; i >= 0; i--)
{
try
{
// create a new list node
ListNode newlyCreatedNode = new ListNode(arrayToReverse[i]);
// calculate how many 'nexts' I need
String codeToExecute = returnNumberOfNexts(j);
// set the next field with the list node I just created
engine.eval(codeToExecute);
}
catch (Exception e)
{
System.out.println(e);
}
j++;
}
}
return finalNode;
}
public static String returnNumberOfNexts(int nextCounter)
{
String finalDotNextString = "finalNode";
if (nextCounter > 0)
{
for (int i = 0; i < nextCounter; i++)
{
finalDotNextString += ".next";
}
}
finalDotNextString += " = newlyCreatedNode;";
return finalDotNextString;
}
The solution above returns the right code to execute, but when it comes to execution, I get:
javax.script.ScriptException: ReferenceError: "finalNode" is not defined in <eval> at line number 1
How do I define finalNode and newlyCreatedNode variables in the engine?
ScriptEnginefor this? Just call.next()in a loop by yourself..nextmember of finalNode. I need to link them all.nextof the last element. You can assign the result of.nextto a variable and call.nexton that variable in a loop (setting the variable to the result every time)