Linked lists

Single linked lists

Single linked lists

A linked list is a collection of nodes. Each with a value and a pointer to the next element in the list.

If the element is the last in the linked list, the pointer is a null pointer.

The first item is the "head".

Traversing the linked list

Start at the head, and then get the pointer, go to that location and continue.

Advantages and disadvantages of linked lists over arrays

Advantages of linked lists include: + Can insert into the list without moving other elements, just changing the pointers. + Can add to the end of a list without needed the next physical space to be available.

Disadvantages of linked lists include: + Takes time (\(O(n)\)) to traverse. + Pointers take up memory.

Write operations on arrays

Inserting to and removing from linked lists

An element can be inserted into a linked list by changing the pointer prior to the element to the element, and setting the pointer for the new element to what would have previously been the next element.

An element can be removed by setting the pointer for the prior element to the element after the one being removed. Note that this means the "deleted" data still exists, it is just not available in the list.

Operations involving multiple lists

Slicing linked lists

Filtering linked lists

Merging sorted linked lists

Concatenating linked lists