easy +5 pts

Cycle a List Once

Move the last element of a list to the front in a single rotation.

Write a function `cycle_list_once(lst)` that takes a list `lst` and returns a new list with the last element moved to the front. For example, `[1,2,3,4]` becomes `[4,1,2,3]`. If the list is empty, return an empty list. If the list has one element, return a copy of it. Do not modify the original list.

Constraints

- `lst` can be empty or contain any number of elements. - The elements can be of any type. - Time complexity: O(n), where n is the length of the list. - Space complexity: O(n) for the output list.

Example

>>> cycle_list_once([1,2,3,4])
[4,1,2,3]
>>> cycle_list_once([5])
[5]
>>> cycle_list_once([])
[]
5 points ~5 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use slicing to split the list into last element and the rest.
Concatenate the slices in the right order.
Return a new list; don't mutate the input.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.