0

What i want it to reverse a list from bottom to top. I used for loop of i--, but i coudn't make it work even by using the built in Reverse() function.

The rules are:

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

Original:

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

Reverse it to look like:

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

Example Test Case:

function reverseList(list) {
    // return reversedList;
}

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 } } };
5
  • 3
    lucky you can't use reverse ... since there's absolutely no arrays in that data Commented Oct 14, 2020 at 1:49
  • 1
    This is called reversing a linkedlist and is just a coding interview problem that should never have to be used in real life. If this is a real problem in some code you wrote, you are using the wrong data structure. If you are trying to get help on a coding interview, implement the algorithm here(geeksforgeeks.org/reverse-a-linked-list) Commented Oct 14, 2020 at 1:54
  • Thank you @KyleDePace, ya, it is a question i was given to solve. Do you think it can be solved easily? maybe with the use of one or two functions? Commented Oct 14, 2020 at 2:09
  • Reverse a nested Array list Javascript as Jaromanda already said, there's no array anywhere in that data structure. That's a linked list. where did you get the term nested Array list from? 4. You must use at least one array to solve the problem. why? that's unnecesary overhead. Commented Oct 14, 2020 at 2:26
  • "it is a question i was given to solve." - Then it is either an interview Question or School learning task. Either way this is a task for you to research. I would suggest you could start by reading about Object.keys() Commented Oct 14, 2020 at 2:34

1 Answer 1

0

there is no array so you cant inbuilt reverse function. also searching for reverse a linked list on SO gave me this which I am referencing in code snippet.

var reverseLinkedList = function(linkedlist) {
  var node = linkedlist;
  var previous = null;

  while (node) {
    // save next or you lose it!!!
    var save = node.next;
    // reverse pointer
    node.next = previous;
    // increment previous to current node
    previous = node;
    // increment node to next node or null at end of list
    node = save;
  }
  return previous; // Change the list head !!!
}
var list = {
  value: 1,
  next: {
    value: 2,
    next: {
      value: 3,
      next: null
    }
  }
};
var linkedlist = reverseLinkedList(list);
console.log(JSON.stringify(linkedlist));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.