I Learnt A NEW Way to Traverse Linked Lists!

Vikram Nayyar CS
4 min readOct 10, 2023

--

Introduction:

Hello all!

This week’s blog post will be a bit shorter but it uses something I’ve encountered recently to then be able to share the benefits of reading other people’s code!

A lot of the time, we are (rightly) taught to focus on our own code and this is great but it means that we’re limited strictly to what we know.

In reality, there are many different approaches that can be taken to solve the same problem (and of course, some are better than others!).

Like I’ve already stated, this week’s post aims to showcase how I was able to learn about a new technique in order to Traverse Linked Lists.

As always, before we get into the article, please make sure to comment, applaud and share this post with your friends!

Happy Reading!!

Quick Information:

I’m using LeetCode’s definition of a Linked List here.

Original Way:

Here’s the approach I’ve typically used for traversing a Linked List.

In my mind, it’s fairly self explanatory.

Check if there’s a node (because you should start off with a node otherwise there’s no point traversing).

Next add that node’s value to a list (typically I want to manipulate the values rather than worry about the Linked List itself).

The 3rd step is “the actual recursion”. We repeat the algorithm for “the next” node. This is also where the “if node” check should make a bit more sense because if we’ve reached the end of the list then the inner block won’t run.

Overall I like this because it doesn’t use any loops and it was the first solution that I was able to come up with “that worked!”.

New Way:

Even though this example is from LeetCode, I’d actually found this solution on CodeWars at the point where I had completed a Kata and was able to view the submissions that other people had made.

To run through this solution quickly, we keep adding the value of the current node to the list, updating the head each iteration until we reach the end of the list.

First things first, once you’ve read this approach, there’s no doubt that it’s more concise (literally no separate function required) and the whole block is only 3 lines.

With that being said, there are a few things that I personally dislike (this may not apply to you):
Firstly, I try to avoid using while loops wherever possible. It’s mainly just a preference thing and they’re normally a last resort to me.
Secondly, I don’t like modifying the head parameter. It just feels weird to me although admittedly the result seems to be the same so perhaps that’s a “me problem”.

I attempted this solution for some other CodeWars Katas. For most problems, this solution worked fine however for some, the time limit was being exceeded due to the while loop.

The Overall Message:

So I’ve touched on a true learning experience of mine which has now given me another technique for tackling Linked Lists however I hope this answers the question “Should I look at other people’s solutions?”

The answer to that is YES.

As the famous saying goes “To steal from 1 person is copying … to steal from many people is research!”.

It’s easy to ramble on about this topic but I’ll summarise it here: When you close off other people’s approaches, you only know what you know or in other words you don’t know what you don’t know.

There could be a whole different world of techniques that you could be using however you haven’t opened up to them therefore you can’t take advantage of them.

Final Things:

As always, thank you for taking the time to read this week’s blog post!

It was quite nice to touch on a general topic with my own story too. Maybe I’ll do this more often?!

All my links are here!

--

--