DEV Community

Xue
Xue

Posted on

The DFS Copy Trap I discovered in Leetcode 113

What's the difference between paths.append(path) and paths.append(list(path)) where path is a list?

paths.append(path) put the original path into paths. If you changed path later, it will affect paths as well.

While paths.append(list(path)) uses list(path) to create a new list object, which remains the same even if you change path in future.

Top comments (0)