


Not all Doubly linked list implementations keep track of the size persistently, however this is one of the optimizations that allows us to take advantage of the directional traversal offered by doubly linked lists. Given a line of size n, we can work out that if i > ( n / 2 ), starting at the back of the line and counting down to the ith will always make finding what number the ith person has faster. You may be curious what role the size of the line has in this knowing the size of the list, we can cut in half the maximum number of people we need to ask. The goal: given the size of the line, find what number the ith person has in their pocket as fast as possible. Also unlike last time, we want to find what number the ith person in line has, given the size of the line and the ability to start at the beginning or end. Unlike last time, each person is able to talk to the next and previous person in line. Once again, we have a group of strangers in a single-file line, and each person has a piece of paper in their pocket with a number on it. To help conceptualize doubly linked lists, we'll borrow a page out of the example given from the singly linked list discussion. References to the previous and next nodes of the list.īefore going forward, it should be noted that, while we are only adding one more pointer, we are also effectively doubling the amount of work that we as developers need to perform to make sure the references are also in check! Conceptualizing Doubly Linked Lists AssumptionsĪ Doubly Linked List is a linked list structure composed of nodes that contain references to the next and previous nodes, allowing for traversal in both directions, as opposed to singly linked lists which can only traverse in a forward direction.Ī doubly linked list node is similar a singly linked list node, containing an additional reference to the node that comes before itself. But when faced with a situation where a linked list structure is your only option, doubly linked lists are able to swoop in and save the day. This is one of the drawbacks the singly linked lists present compared to arrays.

However, when all you have at your fingertips is the "next" node in the list, completing this kind of operation can become a monstrously inefficient task to the order of O(n) time to complete. Sometimes when you are using a linked list structure, you may find yourself needing the "previous" node.
