educative.io

Can't we simply use a stack to eliminate the time taken by 'remove'?

I think the ‘remove’ method used on the list takes a fair bit of time as the time complexity for removing a list item is O(n)
Instead, if we use a stack, and instead of remove, we use pop which takes O(1), we can optimize the solution a bit
We can simply add the stack to the allPaths list when needed by doing new ArrayList<>(stack)

We’ve already used pop_back function to remove the node and its time complexity is O(1).