Alright, today I bumped into a problem called “sword 6”. It’s about reversing a linked list and returning the values in an array. Sounds easy enough, right? So I rolled up my sleeves and got to work.
First, I thought, “Okay, I’ll just iterate through the linked list and store the values in a list. Then, I can simply reverse the list and return it as an array.” Seemed like a solid plan. I started coding, creating an empty list called values.
Then, I used a while loop to go through each node of the linked list. Inside the loop, I grabbed the value of the current node and added it to my values list. I kept doing this until I reached the end of the linked list, which is when the next pointer is null.

- Create an empty list called values.
- Iterate through the linked list.
- Grab the value of each node.
- Add the value to the values list.
- Stop when the next pointer is null.
After the loop, I had all the values in my list, but in the original order. No worries, I just used the reverse() method to flip the list. Finally, I converted the list to an array and returned it.
Here’s the rough sketch of what I did:
I created a list, iterated through the linked list, added values to the list, reversed the list, and returned it as an array. Easy peasy!
It worked like a charm! The code passed all the test cases, and I was pretty happy with myself. It wasn’t the most complicated problem, but it was a good exercise to practice linked lists and basic list manipulation. I guess the takeaway is, sometimes the simplest solution is the best one. You don’t always need fancy algorithms or data structures. Just keep it simple, and you’ll be fine.