CS61B Guide
  • This site is now deprecated
  • Object Oriented Programming
    • Inheritance
    • Access Control
    • Dynamic Method Selection
    • Java Objects
    • Generic Types
  • Asymptotics
    • Asymptotic Analysis Basics
    • Amortization
    • Asymptotics Practice
  • Abstract Data Types
    • Collections
      • Arrays
      • Linked Lists
      • Sets
      • Stacks and Queues
    • Binary Trees
      • Heaps
      • Balanced BSTs
      • Tries
    • Graphs
    • Hashing and Hash Tables
    • Union Find (Disjoint Sets)
    • Comparables and Comparators
  • Sorting
    • Sorting
  • Algorithms
    • Minimax Algorithm
    • Searching
      • Binary Search
      • Depth First Search (DFS)
      • Breadth First Search (BFS)
    • Shortest Paths
      • Dijkstra's Algorithm
      • A* Search
    • Minimum Spanning Trees
      • Prim's Algorithm
      • Kruskal's Algorithm
  • Misc Topics
    • Modular Arithmetic and Bit Manipulation
    • Exceptions
    • More Resources
Powered by GitBook
On this page
  • Feature List of an Effective Linked List
  • Method List
  • Limitation: Arbitrary Retrieval
  • The Java List Interface

Was this helpful?

  1. Abstract Data Types
  2. Collections

Linked Lists

PreviousArraysNextSets

Last updated 5 years ago

Was this helpful?

This page assumes prior knowledge of linked lists from CS61A or equivalent. I'll assume you have already worked with basic singly linked lists before.

The linked list is an extremely common recursive data structure that allows storage and access of an arbitrary amount of data.

Feature List of an Effective Linked List

  1. Rebranding- represents Node as an individual object rather than having one monolithic List type.

  2. Bureacracy: Create an abstraction barrier so that users do not need to know how methods or Nodes work, only how to call them.

  3. Access Control: Data cannot be accessed directly to prevent dangerous behavior; only the provided methods are used.

  4. Nested Class: Nodes are nested within the List object since other classes do not need it.

  5. Caching: The size of the list is incremented every time a node is added, so running size() is O(1) and traversal is not needed.

  6. Generalizing: A sentinel node represents an empty list and remains the first node of the list. When getFirst() is called, the second node is actually returned (since the first node is always the sentinel).

  7. Doubly Linked: Nodes have both first and last pointers for even faster traversal.

  8. Circular list: Sentinel last pointer points to the last value in the node, allowing for fast removeLast().

Method List

Method

Description

Optimal Runtime

addFirst(T x)

addLast(T x)

Adds a node to the front/back of the list.

getFirst()

getLast()

Gets the node at the front/back of the list.

removeFirst()

removeLast()

Removes the node at the front/back of the list.

size()

Returns the number of nodes in the list.

contains(T x)

Returns true if the list contains element x.

add(T x, int pos)

remove(T x)

Adds/remove an element at an arbitrary location.

Limitation: Arbitrary Retrieval

The Java List Interface

You may have noticed in the chart above that it takes Θ(n)\Theta(n)Θ(n) time to retrieve arbitrary values from the list. This will get really slow if the list is large! If arbitrary values need to be accessed frequently, are much better.

Java has a built-in LinkedList class so you don't have to implement it yourself! Read up on the to learn more about the specific methods and behaviors provided.

Θ(1)\Theta(1)Θ(1)
Θ(1)\Theta(1)Θ(1)
Θ(1)\Theta(1)Θ(1)
Θ(1)\Theta(1)Θ(1)
Θ(n)\Theta(n)Θ(n)
Θ(n)\Theta(n)Θ(n)
Arrays
official docs
An illustration of an effective linked list.