My Experience Implementing a LinkedList

Woodelin florveus
4 min readAug 5, 2021

What is a LinkedList

I decided to go deeper into data structures and algorithms which led me to buy a course on Udemy, ‘Data Structures & Algorithms’ by “Scott Barret”. The course concept centered around visualizing the problem rather than diving in headfirst. This technique helped a lot when it came to implementing methods. The course focused on Linked List which is a linear data structure. Each element is a separate object that contains a pointer or a link to the next object. In other words, linked lists are connected and operate differently than arrays.

Advantages / Disadvantages

When working with LinkedList there are constraints and limits to what you’re able to do. However, there are advantages such as containing a number of values without overriding the memory. Furthermore, they are used as the foundation for built-in data structures. The disadvantages include random access of elements, unlike arrays that include random access. LinkedList allows you to point to the next element which is a benefit in my opinion

Starting off with a ListNode

The ListNode has a constructor that contains a value as an argument. The value is used to create new nodes. Furthermore next is set to null. This method will be useful when pointing to the nodes. Check out the example below.

Implementing Our Custom Node

Now that we have a base of understanding LinkedLists let’s try to put it in action by implementing a push method. This push method will require a new node, which will be added to the end of our linked list. We can start with creating our list. Similar to our previous example there has to be a constructor that takes in a value that will correlate with the new node. In addition, ahead that equals our new Node, a tail that equals the head, and a length that is set to the value of one. Here’s a clear example below.

Implementing Our Push Method

Once that has been set up, we are ready to implement our push method. The push method will take in a value. That value will be a part of our new node, therefore we can start by creating a constant called newNode. The constant will take in the value that was passed from the argument.

Since we are starting from the head of the list we need to create a scenario if there was no head. If there is not, we will just set both head and tails to the newNode constant that we created. Once that condition has been implemented, it’s time to look at our else statement, which can be tricky. Our else would point to the tail and set it equal to the newNode. Alongside pointing to the tail we would set the tail to the newNode. Our last step would be to return our list and increase the length. Here’s an example below.

Here are some more methods below.

  1. pop(): Deletes a Node at the end of the list.

2. Unshift(): Add a new Node at the beginning.

3. Shift(): Deletes a Node at the beginning of the list.

4. Get(): returns the index of a Node.

--

--