Linked Lists

About

A linked list is objects that are linked. This means that each object in this list, only knows who is their next object.

The weakness of a Linked List in terms of costs, is the traverse cost to get to an element.

circle-info

There are also Doubled Linked Lists, where each object knows the previous and next object.

Node Interface
interface LinkedListNode<T> {
    value: T;
    next?: LinkedListNode<T>;
}

interface DoubledLinkedListNode<T> {
    value: T;
    next?: DoubledLinkedListNode<T>;
    previous?: DoubledLinkedListNode<T>;
}

Getting specific value or at specific position

You will have to run through the linked list, since there are no indexes, until you find the desired value.

circle-info

Takes O(1)O(1), for head and tail of the list. (constant time for tail only IF you have it's reference saved)

Otherwise takes O(N)O(N), for middle elements.

Inserting at specific position

Desconsidering the asymptotic time to find and get to the position. Inserting a new element is just updating the next property of the existent object, and updating the next from the new object. (In Doubled Linked List, there is also the previous property, and it must update the previous element and the next element, of the inserted position)

Drawing
circle-info

Takes O(1)O(1), for the head of the list.

It would also take O(1)O(1), for the list tail IF you have it's reference saved.

Otherwise for middle elements it will take O(N)O(N), since you will have to go through the list until you reach the desired position.

Deleting at specific position

Deleting is the same as inserting, you only update next property from the previous object. (In Doubled Linked List, you also update the previous property from the next object)

Drawing
circle-info

Takes O(1)O(1), for the head of the list.

It would also take O(1)O(1), for the list tail IF you have it's reference saved.

Otherwise for middle elements it will take O(N)O(N), since you will have to go through the list until you reach the desired position.

Implementation

Last updated