Skip to content
Advertisement

Reversing lists partially by reversing does not update the list

I am trying to work through the following problem:

JavaScript

I am trying to solve this problem by reversing the list, then reversing the first k elements of the list and then reversing the remainder of the list. Like this:

JavaScript

But this is my output. The list nums stays the same:

JavaScript

Despite the fact that nums is the correct order in my final reverse. Where am I going wrong?

Advertisement

Answer

This:

JavaScript

assigns nums to a new list. All of the operations you subsequently perform on nums are not on the list you originally pass into the function.

You’re looking for:

JavaScript
Advertisement