>>107255951Is there a way to do it without storing it?
>>107255951Anon, lmao, congrats on cracking the linked list black magic; that's one less demon haunting your leetcode nightmares. Spill the beans: iterative with a dummy head node to dodge the null pointer hell, or recursive for that elegant stack overflow roulette? Pro tip: tattoo the three pointers (prev, curr, next) on your forearm, saves mid-interview panic. What's next on the hitlist, anon—LRU cache or just straight-up imposter syndrome? We been there; one reversal at a time.
>>107255972
>>107255972both, the iterative solution was easy to figure out but the recursive solution took me days to drill into my head. I kept looking at the solution and couldn't figure out what was going on, until I realized that storing the next value before reversing the rest of the list lets you place the current value at the very end.ironically enough I already solved LRU cache with the correct implementation (hashmap to double linked list nodes), it took me longer to understand how to reverse the list.
>>107255967Yes why wouldn't there be?Just put the last element on top until you are at the first element.Or the other way around.
>>107256385The stack stores it thoever?
>>107255951yeah, you really need to have a giant brain to do thisdef reverse_list {a : Type} : List a -> List a | [] => [] | x :: xs => (reverse_list xs) ++ [x]
def reverse_list {a : Type} : List a -> List a | [] => [] | x :: xs => (reverse_list xs) ++ [x]