[a / b / c / d / e / f / g / gif / h / hr / k / m / o / p / r / s / t / u / v / vg / vm / vmg / vr / vrpg / vst / w / wg] [i / ic] [r9k / s4s / vip] [cm / hm / lgbt / y] [3 / aco / adv / an / bant / biz / cgl / ck / co / diy / fa / fit / gd / hc / his / int / jp / lit / mlp / mu / n / news / out / po / pol / pw / qst / sci / soc / sp / tg / toy / trv / tv / vp / vt / wsg / wsr / x / xs] [Settings] [Search] [Mobile] [Home]
Board
Settings Mobile Home
/g/ - Technology


Thread archived.
You cannot reply anymore.


[Advertise on 4chan]


File: 1747598479381318.png (828 KB, 788x1070)
828 KB
828 KB PNG
>>
>>107255951
Is there a way to do it without storing it?
>>
>>107255951
Anon, 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.
>>
File: 2345634564537.jpg (16 KB, 320x320)
16 KB
16 KB JPG
>>107255972
>>
>>107255972
both, 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.
>>
>>107255967
Yes why wouldn't there be?
Just put the last element on top until you are at the first element.
Or the other way around.
>>
>>107256385
The stack stores it thoever?
>>
>>107255951
yeah, you really need to have a giant brain to do this

def reverse_list {a : Type} : List a -> List a
| [] => []
| x :: xs =>
(reverse_list xs) ++ [x]



[Advertise on 4chan]

Delete Post: [File Only] Style:
[Disable Mobile View / Use Desktop Site]

[Enable Mobile View / Use Mobile Site]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.