tried to use prev pointers, but seems like the answer is the same