Yes, I think the time complexity is O(n). The space complexity is O(n) as well, because we are creating 1 new node when we visit a particular node and at last we are visiting each node once.
I request author to look into this and also provide lucid explanation instead of only giving pseudocode and code (may be add visualization or something like that).