
Problem: List Reverse in JavaScript
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:
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.
Hints:
- Try breaking down the problem into multiple functions based on the exercises from the textbook code sandbox problems. The following would potentially be the functions in your program:
- Turn the list into an array -> listToArray(...)
- Reverse the array -> reverseArray(...)
- Turn the array into a list -> arrayToList(...)

Trending nowThis is a popular solution!
Step by stepSolved in 3 steps with 1 images

- ASSUMING C LANGUAGE True or False: You can have the data portion of a Linked List be a Struct containing a Linked List itselfarrow_forwardC++arrow_forwardIn C++ commas must be there. main.cpp #include <vector>#include <string>#include <iostream> using namespace std; // TODO: Write method to create and output all permutations of the list of names.void PrintAllPermutations(const vector<string> &permList, const vector<string> &nameList) { } int main() { vector<string> nameList; vector<string> permList; string name; // TODO: Read in a list of names; stop when -1 is read. Then call recursive method. return 0;}arrow_forward
- please code in python The following code defines a list of names and also contains a header for the function best. The best function takes two arguments: a generic scoring function, score, and a list of strings, names. Fill in the function so that it applies a score function to each string in the names list and returns the name with the highest score. If there are ties in the list, your function should return the first item with the maximum score. The best function needs to be designed so that it can take any scoring function and return the name with the highest score. For this question, define a function called len_score that returns the length of a word. Call the best function with the len_score function as a parameter. Example: names = ["Ben", "April", "Zaber", "Alexis", "McJagger", "J.J.", "Madonna"] best(len_score, names) -> 'McJagger'arrow_forwardThe program requires me to remove all item for a list, but when doing the test case for it, it only removes one. What would be the problem in this code?Given code: #include "MovieList.h"#include <iostream> using namespace std;// Constructor functionMovieList::MovieList() { top = nullptr; } // Destructor functionMovieList::~MovieList() { MovieNode *p = nullptr; while (top->next != nullptr) { p = top; top = top->next; delete p; } delete top; p = nullptr; top = nullptr; } void MovieList::display(ostream &out) { MovieNode *t = top; int pos = 0; // Function to display the movie list while (t != nullptr) { out << pos << ": " << t->title << endl; pos++; t = t->next; } } int MovieList::count() { MovieNode *t = top; int pos = 0; // Counting based off the movie list while (t != nullptr) { pos++; t = t->next; } return pos; } void…arrow_forwardvoid listEmployees (void) { for (int i=0; i 10000. Make a guess about why the comparison function takes 2 struct Employee parameters (as opposed to struct Employee *) **arrow_forward
- struct insert_at_back_of_sll { // Function takes a constant Book as a parameter, inserts that book at the // back of a singly linked list, and returns nothing. void operator()(const Book& book) { /// TO-DO (3) /// // Write the lines of code to insert "book" at the back of "my_sll". Since // the SLL has no size() function and no tail pointer, you must walk the // list looking for the last node. // // HINT: Do not attempt to insert after "my_sll.end()". // ///// END-T0-DO (3) ||||// } std::forward_list& my_sll; };arrow_forwardWrite a function in_list that will take as its parameters an input list called mylist and a number. If the number is in the list the function should retun true, ottherwise, it should retun false. def in_list(mylist,number):arrow_forwardSuppose a node of a doubly linked list is defined as follows: struct Node{ int data; struct Node* next; struct Node* prev; }; Write the function definition of the function deleteElement as presented below. This function deletes a node at position n from a doubly linked list. struct Node* deleteElement(struct Node* head, int n){ //write the function definition }arrow_forward
- struct nodeType { int infoData; nodeType * next; }; nodeType *first; … and containing the values(see image) Using a loop to reach the end of the list, write a code segment that deletes all the nodes in the list. Ensure the code performs all memory ‘cleanup’ functions.arrow_forwardcode in c programarrow_forwardC++ The List class represents a linked list of dynamically allocated elements. The list has only one member variable head which is a pointer that leads to the first element. See the following code for the destructor to List. ~ List () { for (int i = 0; i <size (); i ++) { pop_back (); } } What problems does the destructor have? Select one or more options: 1. There are no parameters for the destructor. 2. The return value from pop_back (if any) is nerver handled. 3. The destructor will create a stack overflow. 4. The destructor will create dangling pointers. 5.The destructor will create memory leaks. 6.The destructor will create undefined behavior (equivalent to zero pointer exception). 7.The condition must be: i <size () - 1 8. There is at least one problem with the destructor, but none of the above.arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





