I'm a JS Subject Matter Expert (SME) that has spent the past few years spearheading curricula and teaching initiatives at colleges and bootcamps, in person and virtually.
Could you elaborate with an example? I have been struggling with these concepts for a couple of days now. I went about trying to turn a linked list into an array and reversing it and then building it back into a linked list, but that's definitely not ideal, although it does work for most cases.
The example in this article seems a bit contrived with the .next.next - so it may only apply for 'short lists.'
Any other insight is appreciated as to how we can reliably reverse a linked list (in JS) in a 'clean', modern functional way.
I'm a JS Subject Matter Expert (SME) that has spent the past few years spearheading curricula and teaching initiatives at colleges and bootcamps, in person and virtually.
The key insight here ought to be that a linked list satisfies the requirements of a stack.
Which means that you can reverse a linked list by popping off the source list and pushing onto the destination list.
There's no need to introduce cycles by using a back-link.
Could you elaborate with an example? I have been struggling with these concepts for a couple of days now. I went about trying to turn a linked list into an array and reversing it and then building it back into a linked list, but that's definitely not ideal, although it does work for most cases.
The example in this article seems a bit contrived with the
.next.next
- so it may only apply for 'short lists.'Any other insight is appreciated as to how we can reliably reverse a linked list (in JS) in a 'clean', modern functional way.
Using a stack abstraction
Or, if you prefer recursion
I prefer recursion. Tx.