Medium · Backtracking Choices
Understand → Visualise → Practice.
Return every permutation of the distinct input values.
Build permutations one position at a time. At each level, try every value not yet used, recurse, then undo the choice.
Keep the current partial permutation and which values are still available.
For every remaining value, append it and recurse on the rest.
When no values remain, the path is a complete permutation; save a copy.
O(n × n!) time and O(n) recursion depth, plus the output.
Saving the path list itself instead of a copy leaves every result pointing at the same changing list.
Read the full guide first: Recursion and Backtracking.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.