Nodes & Linked Lists
Essential Questions
What are the qualities of Graphs
What are the tradeoffs between linked lists and arrays?
What are the tradeoffs between singly linked lists and doubly linked lists?
What are the run times for insertion, deletion, and accessing from linked lists?
Key Terms
Graph
Node
Singly linked list
Doubly linked list
Random access
Sequential access
Nodes
In the Stack and Queue data structure,
A Graph is a category of abstract data type that is used to organize relationships between data.
The thing that all graphs share is that they are comprised of nodes that hold a single piece of data, and edges that connect two nodes.
Q: Consider the abstract data structures below. What do the nodes in each structure point to?
Linked Lists

Doubly Linked Lists

Trees

Making a Node Class for Linked Lists
Nodes themselves typically do not have any methods.
The simplest kind of node is the one used in a linked list. It holds its own data and a pointer to the next node in the list.

Q: What is the head of the linked list? What is the tail?
Making a Linked List Class

The linked list itself holds only a reference to a head node and various methods for modifying or "traversing" the list.
"Traversing" is a fancy word for "visiting the nodes in a particular order" in a data structure.
Q: What is the way/what are the ways that we can traverse a linked list?
Some linked lists may also implement:
adding a new node in the middle
removing a node from the middle
Let's visualize: https://visualgo.net/en/list
Algorithm: Prepend to head
Inputs: data to add
Output: the new
headof the linked listBehavior: the new node should be the new
headof the linked list and it should point to the previousheadof the linked list
Solution
The new node is going at the beginning of the list. So it's
nextpointer should point to the existingheadof the list.Then, the list's
headpointer should now point at the new node.Test:
Adding to a list with multiple nodes
Adding to an empty list
Adding to a list with one value
Algorithm: Append to tail
Inputs: data to add
Output: the
headof the linked listBehavior: the previous tail node's
nextproperty should point to the new node.
Solution
To put the new node at the end of the list, we need to first get to the end of the list, starting at the list's
head. We'll use acurrNodevariable to keep track of where we are in the list.Using a
whileloop, we iterate as long as thecurrNodehas anextnode to move to.We'll reach the tail node once
currNodehas nonextnode. At this point, we set thecurrNode(which is the tail) to point to the new node.Test:
Adding to a list with multiple nodes
Adding to an empty list
Adding to a list with one node
Algorithm: isCyclic
This is not a method of linked lists but a method whose input is the head of a linked list. It should return true if the linked list contains a cycle, false otherwise.
Last updated