reading-notes

Code Fellows Notes

View the Project on GitHub stephnitis/reading-notes

Linked Lists

Linked Lists

What’s a Linked List, Anyway? [Part 1]

Singly linked lists are the simplest type of linked list, based solely on the fact that they only go in one direction. There is a single track that we can traverse the list in; we start at the head node, and traverse from the root until the last node, which will end at an empty null value.

Doubly linked list: has two references contained within each node: a reference to the next node, as well as the previous node. This can be helpful if we want to be able to traverse our data structure not just in a single track or direction, but also backwards.

A circular linked list is a little odd in that it doesn’t end with a node pointing to a null value. Instead, it has a node that acts as the tail of the list (rather than the conventional head node), and the node after the tail node is the beginning of the list. This organization structure makes it really easy to add something to the end of the list, because you can begin traversing it at the tail node, as the first element and last element point to one another.

What is a Linked List Anyway? Part 2

O(1) function takes constant time, which is to say that it doesn’t matter how many elements we have, or how huge our input is: it’ll always take the same amount of time and memory to run our algorithm.

O(n) function is linear, which means that as our input grows (from ten numbers, to ten thousand, to ten million), the space and time that we need to run that algorithm grows linearly.

O(n²) function, which clearly takes exponentially more time and memory the more elements that you have.

Grow a linked List, rearrange our pointers:

  1. First, we find the head node of the linked list.
  2. Next, we’ll make our new node, and set its pointer to the current first node of the list.
  3. Lastly, we rearrange our head node’s pointer to point at our new node.

Inserting an element at the beginning of a linked list is particularly nice and efficient because it takes the same amount of time, no matter how long our list is, which is to say it has a space time complexity that is constant, or O(1).

Inserting an element at the end of a linked list:

  1. Find the node we want to change the pointer of (in this case, the last node)
  2. Create the new node we want to insert and set its pointer (in this case, to null)
  3. Direct the preceding node’s pointer to our new node

Finding the last node means that we need to traverse through the entire linked list.

Because linked lists are distributed, non-contiguous memory so traversing is going to take time — it’s going to take more time with the more nodes we have.

The space time complexity this type of inserting will leave us with is a linear O(n). This insert algorithm would take as much time as the number of elements in our list

a linked list is usually efficient when it comes to adding and removing most elements, but can be very slow to search and find a single element.

Linked Lists Analogy as Ms Pac-Man

Linked List Repl