Question

In: Computer Science

Javascript Problem: List Reverse Given a list: var list = { value: 1, next: { value:...

Javascript Problem: List Reverse

Given a list:

var list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: null     
    }
  }
};

Reverse the order of the list so that it looks like:

var list = {
  value: 3,
  next: {
    value: 2,
    next: {
      value: 1,
      next: null
    }
  }
};

Use the following shell if needed:

//assignment1.js

function reverseList(list) {
    // your code here
    ...
    return reversedList;
}

Example Test Case(s):

Arguments: { value: 1, next: { value: 2, next: { value: 3, next: null } } };
Returns: { value: 3, next: { value: 2, next: { value: 1, next: null } } };

Arguments: { value: "a", next: { value: "b", next: { value: "c", next: null } } };
Returns: { value: "c", next: { value: "b", next: { value: "a", next: null } } };

Requirements:

  • You cannot use the built-in reverse() function.
  • null is non-existent object, you may use an empty object instead if desired.
  • Your function should be able to reverse a list of values of any type.
  • You must use at least one array to solve the problem.

Solutions

Expert Solution

// Javascript program to reverse the order of the list

// function to reverse the order of the input list and return the reversed list
function reverseList(list) {
  
   // create an empty reversedList
   reversedList = {};
  
   // create an empty array to store the elements of the list
   data = [];
  
   // set curr to list i.e start of the list
    curr = list;
  
   // loop that continues till the end of the list i.e until null or empty object is not encountered
   while((curr != null) && (Object.keys(curr).length != 0))
   {
       data.push(curr.value); // insert curr's value at the end of data
       curr = curr.next; // move curr to next node
   }
  
   // set curr to reversedList start pointer
   curr = reversedList;
  
   // loop over the data array in reverse order i.e from last to first
   for(var i=data.length-1;i>=0;i--)
   {
       // reversedList is an empty object
       if(Object.keys(reversedList).length == 0){
           // set reversedList list value to ith element of data and next to null
           reversedList =    {value:data[i], next: null};
           curr = reversedList; // set curr to reversedList
       }  
       else // reversedList is not an empty object
       {  
           // set value and next for node next to curr
           curr.next = {value:data[i], next:null};
           curr = curr.next; // move curr to next node
       }  
   }
  
return reversedList;
}

// test the function
var list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: null
}
}
};

console.log(reverseList(list));

var list = { value: "a", next: { value: "b", next: { value: "c", next: null } } };
console.log(reverseList(list));

//end of program

Output:


Related Solutions

/*     Here is an example of a conditional in JavaScript */ var numberOfParticipants = 5; var...
/*     Here is an example of a conditional in JavaScript */ var numberOfParticipants = 5; var maxNumberOfParticipants = 10; if (numberOfParticipants < maxNumberOfParticipants) {     console.log("You can add another participant now"); } else {     console.log("Sorry, you have reached the max number of participants and cannot add another"); } /*     Now it's your turn - create a simple conditional like my example. You can setup variables like I did or just use     hard-coded numbers */ /*     Here is an example of creating...
<script type = "text/javascript">     var  first_number = prompt("Please enter the first number for our sequence calculation");     var  second_number...
<script type = "text/javascript">     var  first_number = prompt("Please enter the first number for our sequence calculation");     var  second_number = prompt("Please enter the second number for our sequence calculation");     first_number = parseInt(first_number);     second_number = parseInt(second_number);     console.log(first_number);     console.log(second_number);     var previous_number = first_number;     var next_number = second_number;     var count = 10;     while(count>0)     {         count = count - 1;         var new_number = previous_number + next_number;         previous_number = next_number;         next_number = new_number;         console.log(next_number)     } I need to use the fib function to print the same number sequence...
Illustrate the difference between the value-at-risk (VaR) and conditional value-at-risk (C-VaR) measures
Illustrate the difference between the value-at-risk (VaR) and conditional value-at-risk (C-VaR) measures
JavaScript Given the following object, log every property name and value to the console using a...
JavaScript Given the following object, log every property name and value to the console using a loop. let myObj = { id: 12 name: 'My Object', class: 'obj', height: 65, likeJavascript: true, data: [1, 53, 23] };
Given: E[x] = 4, E[y] = 6, Var(x) = 2, Var(y) = 1, and cov(x,y) =...
Given: E[x] = 4, E[y] = 6, Var(x) = 2, Var(y) = 1, and cov(x,y) = 0.2 Find a lower bound on (5 < x + y < 10). State the theorem used.
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1...
import sys var = 1 print(var) def function1(myVar):     print(myVar)     var = myVar + 1     print(var)     function2(var) def function2(myVar):     print(myVar)     var = myVar + 1     print(var)     function3(var) def function3(myVar):     print(myVar)     var = myVar + 1     print(var) def main(argv):     var = 10     print(var)     function1(var) if __name__=="__main__":     main(sys.argv) 1. As the program runs, what is the first line that will be interpreted by Python, and what action will...
[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list...
[Javascript] Create a function(returnObjectFromId(case, ...idNum)) to return the case Object(s) for a given idNum, or list of idNums. Calling with a single `idNum` value should return the case Object, and return NULL if an id value that's unknown is passed returnObjectFromId(case, 84838) would return the Object in the cases Array with an 'idNumber' of id, and use the .find() method of the cases Array to locate items by idNumber. returnObjectFromId(cases, -23298312) would return null. returnObjectFromId(cases, 161020, 161021) would return an...
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1)...
/* Assignment: Complete this javascript file according to instructions given in the comments. */ // 1) Declare a variable named myName equal to your first name //Firstname is Susan // Construct a basic IF statement that prints the variable to the // console IF the length of myName is greater than 1 // 2) Copy your IF statement from above and paste it below // Change the IF statement to check if the length of myName // is greater than...
      Show that     Var(x-a/)=1/h^2 Var(x)
      Show that     Var(x-a/)=1/h^2 Var(x)
VAR Measures the potential maximum 1-day loss on the value of positions of an MNC that...
VAR Measures the potential maximum 1-day loss on the value of positions of an MNC that is exposed to exchange rate movements. Basically, it goes to compute how much is the maximum possible loss of MNC going to be if exchange rates move against the firm position. In Calculating the Value at Risk (VAR), what is the implication of underestimating volatility of exchange rates? ( Please respond in a complete and explanatory way ) Answer should have 100 words minimum
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT