Welcome back! Next we’re going to wrap up our discussion of lists. First, we’ll make some forward progress on our linked list implementation. Then, we’ll compare and contrast the performance of our two approaches.
But! Let’s warm up with another graded debugging challenge!
Declare a public class Bracketer providing one static method create.
create takes a single int parameter and returns an anonymous object that implements the Bracket interface:
The returned object should implement top so that it returns the passed int and bottom so that it returns the
passed int * -1.
So, for example:
At this point we can initialize our linked lists and get and set items. But what about add and remove? It’s not a list yet!
Both add and remove require adjusting the linkage to either insert or delete elements. This is tricky! Let’s start with remove. First, let’s look at what needs to happen using a diagram.
OK, now let’s take a stab at this in code!
What about add? We’ll leave that for you to work on… But, to help you get started, let’s diagram it first:
ArrayList v. LinkedList PerformanceArrayList v. LinkedList PerformanceLet’s examine the performance tradeoffs of our ArrayList versus our LinkedList.
Here’s that comparison in one place, for the implementations we built. Notice that it does not simply say that arrays are fast and linked lists are slow: which one wins depends on the operation.
| Operation | Array list | Linked list |
|---|---|---|
get and set | O(1) | O(n) |
add and remove in general | O(n) | O(n) |
add or remove at the front | O(n) | O(1) |
add at the end | O(n) | O(n), or O(1) with a reference to the end |
Now, you might be wondering why anyone would use a linked list, ever! However, there are applications that only ever modify the ends of the list!
For example, consider a help queue. Requests are added at one end and removed from the other. So we are only ever modifying the front or the end of the list.
Modifying the front of a linked list is O(1).
But what about the end?
Right now adding to the end of our linked list is O(n), because we have to walk the entire list to find the end.
That’s the same big-O as our array list, but for a different reason: the array list has to copy every item.
So if you time adding to the end and see it slow down as the list grows, that alone doesn’t tell you which kind of list you have.
Could we make that O(1) too?
Sure!
Let’s see how.
Note that this approach allows us to implement adding to the end of a linked list in O(1). Now the two lists behave differently: the array list still slows down as it grows, and the linked list doesn’t.
What about if we also wanted to support efficient removal from the end? We’d need to make one additional change…
Starting with the SimpleLinkedList class provided below, complete the code for add.
You'll want review the rest of the code to understand how this list implementation works and how to
walk a linked list and manipulate the references properly.
add takes the position to add at as an int as its first parameter and the Object
reference to add as its second.
add should add the element to the list, increasing the size by one and shifting
elements after the add position backward.
You should assert that the passed position is valid for this list.
But note that you should allow adding a new item to the end of the existing list.
When you are done, here is how your SimpleLinkedList class should work:
Increasingly, important decisions about our lives are being made by algorithms. Whether you get a job. Who you meet, get to know, and fall in love with. When you’re in trouble, whether you get extra help or are left to struggle. If you can get a loan to buy that home, or need to live somewhere else.
Cathy O’Neil has played an important role drawing attention to the ways in which algorithmic decision making is shaping and misshaping our lives. Her foundational book “Weapons of Math Destruction” explores how this topic intersects with multiple aspects of our lives. In the video below, she discusses what we should expect from the increasingly mysterious algorithms that seem destined to continue to shape our lives:
Need more practice? Head over to the practice page.