Skip to main content
Engineering LibreTexts

5.4: Doubly-linked list

  • Page ID
    12753
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    My implementation of a linked list, MyLinkedList, uses a singly-linked list; that is, each element contains a link to the next, and the MyArrayList object itself has a link to the first node.

    But if you read the documentation of LinkedList at thinkdast.com/linked, it says:

    Doubly-linked list implementation of the List and Deque interfaces. [. . . ] All of the operations perform as could be expected for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.

    If you are not familiar with doubly-linked lists, you can read more about them at thinkdast.com/doublelist, but the short version is:

    • Each node contains a link to the next node and a link to the previous node.
    • The LinkedList object contains links to the first and last elements of the list.

    So we can start at either end of the list and traverse it in either direction. As a result, we can add and remove elements from the beginning and the end of the list in constant time!

    The following table summarizes the performance we expect from ArrayList, MyLinkedList (singly-linked), and LinkedList (doubly-linked):

    MyArrayList

    MyLinkedList

    LinkedList

    add (at the end) 1 n 1
    add (at the beginning) n 1 1
    add (in general) n n n
    get / set 1 n n
    indexOf / lastIndexOf n n n
    isEmpty / size 1 1 1
    remove (from the end) 1 n 1
    remove (from the beginning) n 1 1
    remove (in general) n n n

    This page titled 5.4: Doubly-linked list is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) .

    • Was this article helpful?