Linked lists actually come in a number of different flavors. The linked list we’ve discussed until this point is the “classic” linked list, but with some slight modifications, we can grant linked lists additional superpowers.

One variant form of the linked list is the doubly linked list.

A doubly linked list is like a linked list except that each node has two links—one that points to the next node, and another that points to the previous node. In addition, the doubly linked list always keeps track of both the first and last nodes, instead of just the first node.

Here’s what a doubly linked list looks like:

images/pitting_linked_lists_against_arrays/doubly_linked_list.png

We can implement the core of a doubly linked list in Ruby like this:

​  ​ class ​ Node
​  
​  attr_accessor ​ :data ​, ​ :next_node ​, ​ :previous_node ​
​  
​  ​ def ​ ​ initialize ​(data)
​  @data = data
​  ​ end ​
​  
​  ​ end ​
​  
​  ​ class ​ DoublyLinkedList
​  
​  attr_accessor ​ :first_node ​, ​ :last_node ​
​  
​  ​ def ​ ​ initialize ​(first_node=​ nil ​, last_node=​ nil ​)
​  @first_node = first_node
​  @last_node = last_node
​  ​ end ​
​  
​  ​ end ​

Since a doubly linked list always knows where both its first and last nodes are, we can access each of them in a single step, or O(1). So, just as we can read, insert, or delete from the beginning of the list in O(1), we can do the same from the end of the list in O(1) as well.

Here’s a depiction of inserting at the end of a doubly linked list:

images/pitting_linked_lists_against_arrays/insert_into_doubly_linked_list.png

As you can see, we create a new node ( "Sue" ) and have its previous_node point to what used to be the last_node ( "Greg" ) of the linked list. Then, we change the next_node of the last_node ( "Greg" ) to point to this new node ( "Sue" ). Finally, we declare the new node ( "Sue" ) to be the last_node of the linked list.

Code Implementation: Doubly Linked List Insertion

Here’s the implementation of a new insert_at_end method that we can add to our DoublyLinkedList class:

​  ​ def ​ ​ insert_at_end ​(value)
​  new_node = Node.​ new ​(value)
​  
​  ​ # If there are no elements yet in the linked list: ​
​  ​ if ​ !first_node
​  @first_node = new_node
​  @last_node = new_node
​  ​ else ​ ​ # If the linked list already has at least one node: ​
​  new_node.​ previous_node ​ = @last_node
​  @last_node.​ next_node ​ = new_node
​  @last_node = new_node
​  ​ end ​
​  ​ end ​

Let’s highlight the most important parts of this method.

First, we create the new node:

​  new_node = Node.​ new ​(value)

Then, we set the previous_node link of the new_node to point to what until this point was the last node:

​  new_node.​ previous_node ​ = @last_node

Then, we change the last node’s link (which was nil until this point) and have it point to our new_node :

​  @last_node.​ next_node ​ = new_node

Last, we tell our instance of the DoublyLinkedList that its last node is our new_node :

​  @last_node = new_node

Moving Forward and Backward

With a “classic” linked list, we can only move forward through the list. That is, we can access the first node and follow the links to find all the other nodes of the list. But we’re not able to move backward, as no node is aware of what the previous node is.

A doubly linked list allows for a lot more flexibility, as we can move both forward and backward through the list. In fact, we can even start with the last node and work our way backward to the first node.