What is a linked list, and how is it different from an array? How efficient or inefficient are its operations? What are its strengths and weaknesses? How can I construct and interact with a linked list? By the end of this objective, you will be able to answer all of these questions confidently.
A linked list is a simple, linear data structure used to store a collection of elements. Unlike an array, each element in a linked list does not have to be stored contiguously in memory.
For example, in an array, each element of the list [43, 32, 63
is stored in memory like so:
43
is the first item in the collection and is therefore stored in the first slot. 32
is the second item and is stored immediately next to 43
in memory. This pattern continues on and on.
In a linked list, each element of the list could be stored like so:
You can see here that the elements can be spaced out in memory. Because the elements are not stored contiguously, each element in memory must contain information about the next element in the list. The first item stores the data 43
and the location in memory (*3
) for the next item in the list. This example is simplified; the second item in the list 32
could be located anywhere in memory. It could even come before the first item in memory.
You might also be wondering what types of data can be stored in a linked list. Pretty much any type of data can be stored in a linked list. Strings, numbers, booleans, and other data structures can be stored. You should not feel limited using a linked list based on what type of data you are trying to store.
Are the elements in a linked list are sorted or unsorted? The elements in a linked list can be either sorted or unsorted. There is nothing about the data structure that forces the elements to be sorted or unsorted. You cannot determine if a linked list's elements are sorted by determining they are stored in a linked list.
What about duplicates? Can a linked list contain them? Linked lists can contain duplicates. There is nothing about the linked list data structure that would prevent duplicates from being stored. When you encounter a linked list, you should know that it can contain duplicates.
Are there different types of linked lists? If so, what are they? There are three types of linked lists: singly linked list (SLL), doubly linked list (DLL), and circular linked list. All linked lists are made up of nodes where each node stores the data and also information about other nodes in the linked list.
Each singly linked list node stores the data and a pointer where the next node in the list is located. Because of this, you can only navigate in the forward direction in a singly linked list. To traverse an SLL, you need a reference to the first node called the head. From the head of the list, you can visit all the other nodes using the next pointers.
The difference between an SLL and a doubly linked list (DLL) is that each node in a DLL also stores a reference to the previous item. Because of this, you can navigate forward and backward in the list. A DLL also usually stores a pointer to the last item in the list (called the tail).
A Circular Linked List links the last node back to the first node in the list. This linkage causes a circular traversal; when you get to the end of the list, the next item will be back at the beginning of the list. Each type of linked list is similar but has small distinctions. When working with linked lists, it’s essential to know what type of linked list.
Lookup
To look up an item by index in a linked list is linear time (O(n)
). To traverse through a linked list, you have to start with the head reference to the node and then follow each subsequent pointer to the next item in the chain. Because each item in the linked list is not stored contiguously in memory, you cannot access a specific index of the list using simple math. The distance in memory between one item and the next is varied and unknown.
Append
Adding an item to a linked list is constant time (O(1)
). We always have a reference point to the tail of the linked list, so we can easily insert an item after the tail.
Insert
In the worst case, inserting an item in a linked list is linear time (O(n)
). To insert an item at a specific index, we have to traverse — starting at the head — until we reach the desired index.
Delete
In the worst case, deleting an item in a linked list is linear time (O(n)
). Just like insertion, deleting an item at a specific index means traversing the list starting at the head.
Space
The space complexity of a linked list is linear (O(n)
). Each item in the linked list will take up space in memory.
The primary strength of a linked list is that operations on the linked list's ends are fast. This is because the linked list always has a reference to the head (the first node) and the tail (the last node) of the list. Because it has a reference, doing anything on the ends is a constant time operation (O(1)
) no matter how many items are stored in the linked list. Additionally, just like a dynamic array, you don't have to set a capacity to a linked list when you instantiate it. If you don't know the size of the data you are storing, or if the amount of data is likely to fluctuate, linked lists can work well. One benefit over a dynamic array is that you don't have doubling appends. This is because each item doesn't have to be stored contiguously; whenever you add an item, you need to find an open spot in memory to hold the next node.
The main weakness of a linked list is not efficiently accessing an "index" in the middle of the list. The only way that the linked list can get to the seventh item in the linked list is by going to the head node and then traversing one node at a time until you arrive at the seventh node. You can't do simple math and jump from the first item to the seventh.
Remember that linked lists have efficient operations on the ends (head and tail). There are two structures that only operate on the ends; queues and stacks. So, most queue or stack implementations use a linked list as their underlying data structure.
We can see the difference between how a linked list and an array are stored in memory, but why is this important? Once you see the problem with the way arrays are stored in memory, the benefits of a linked list become clearer.
The primary problem with arrays is that they hold data contiguously in memory. Remember that having the data stored contiguously is the feature that gives them quick lookups. If I know where the first item is stored, I can use simple math to figure out where the fifth item is stored. The reason that this is a problem is that it means that when you create an array, you either have to know how much space in memory you need to set aside, or you have to set aside a bunch of extra memory that you might not need, just in case you do need it. In other words, you can be space-efficient by only setting aside the memory you need at the moment. But, in doing that, you are setting yourself up for low time efficiency if you run out of room and need to copy all of your elements to a newer, bigger array.
With a linked list, the elements are not stored side-by-side in memory. Each element can be stored anywhere in memory. In addition to storing the data for that element, each element also stores a pointer to the memory location of the next element in the list. The elements in a linked list do not have an index. To get to a specific element in a linked list, you have to start at the head of the linked list and work your way through the list, one element at a time, to reach the specific element you are searching for. Now you can see how a linked list solves some of the problems that the array data structure has.
Let’s look at how we can represent a singly linked list graphically and in Python code. Seeing a singly linked list represented graphically and in code can help you understand it better.
How do you represent a singly linked list graphically? Let’s say you wanted to store the numbers 1, 2, and 3. You would need to create three nodes. Then, each of these nodes would be linked together using the pointers.
Notice that the last element or node in the linked list does not have a pointer to any other node. This fact is how you know you are at the end of the linked list.
What does a singly linked list implementation look like in Python? Let's start by writing a LinkedListNode
class for each element in the linked list.
Now, we need to build out the class for the LinkedList
itself:
Our class is super simple so far and only includes an initialization method. Let's add an append
method so that we can add nodes to the end of our list:
Now, let's use our simple class definitions for LinkedListNode
and LinkedList
to create a linked list of elements 1
, 2
, and 3
.
You must be able to understand and interact with linked lists. You now know the basic properties and types of linked lists, what makes a linked list different from an array, what problem it solves, and how to represent them both graphically and in code. You now know enough about linked lists that you should be able to solve algorithmic code challenges that require a basic understanding of linked lists.
Draw out a model of a singly-linked list that stores the following integers in order: 3,2,6,5,7,9
.
Draw out a model of a doubly-linked list that stores the following integers in order: 5,2,6,4,7,8
.
There are two ways to sort a linked list using :
Exchanging data between nodes
Modifying the links between nodes
In this section, we will see how both these approaches work. We will use the bubble sort algorithm to first sort the linked list by changing the data, and then we will see how we can use bubble sort to change the links in order to sort the linked list.
Sorting Linked List by Exchanging Data
To sort a linked list by exchanging data, we need to declare three variables p
, q
, and end
.
The variable p
will be initialized with the start node, while end
will be set to None
.
It is important to remember that to sort the list with n
elements using bubble sort, you need n-1
iterations.
To implement bubble sort, we need two while loops. The outer while loop executes until the value of variable end
is equal to the self.start_node
.
The inner while loop executes until p
becomes equal to the end
variable. Inside the outer while loop, the value of p
will be set to self.start_node
which is the first node. Inside the inner while loop, the value of q
will be set to p.link
which is actually the node next to q
. Then the values of p
and q
will be compared if p
is greater than q
the values of both the variables will be swapped and then p
will point to p.ref
, which is the next node. Finally, the end
will be assigned the value of p
. This process continues until the linked list is sorted.
Let's understand this process with the help of an example. Suppose we have the following list:
Let's implement our algorithm to sort the list. We'll see what will happen during each iteration. The purpose of the bubble sort is that during each iteration, the largest value should be pushed to the end, hence at the end of all iterations, the list will automatically be sorted.
Before the loop executes, the value of end
is set to None
.
In the first iteration, p
will be set to 8, and q
will be set to 7
. Since p
is greater than q
, the values will be swapped and p
will become p.ref
. At this point of time the linked list will look like this:
Since at this point of time, p
is not equal to end
, the loop will continue and now p
will become 8 and q
will become 1. Since again p
is greater than q
, the values will be swapped again and p
will again become p.ref
. The list will look like this:
Here again, p
is not equal to end
, the loop will continue and now p
will become 8 and q
will become 6. Since again p
is greater than q
, the values will be swapped again and p
will again become p.ref
. The list will look like this:
Again p
is not equal to end
, the loop will continue and now p
will become 8 and q
will become 9. Here since p
is not greater than q
, the values will not be swapped and p
will become p.ref
. At this point of time, the reference of p
will point to None
, and end
also points to None
. Hence the inner while loop will break and end
will be set to p
.
In the next set of iterations, the loop will execute until 8, since 9 is already at the end. The process continues until the list is completely sorted.
The Python code for sorting the linked list using bubble sort by exchanging the data is as follows:
Add the bub_sort_dataexchange()
method to the LinkedList
class that you created in the last article.
Once you add the method to the linked list, create any set of nodes using the make_new_list()
function and then use the bub_sort_dataexchange()
to sort the list. You should see the sorted list when you execute the traverse_list()
function.
Sorting Linked List by Modifying Links
Bubble sort can also be used to sort a linked list by modifying the links instead of changing data. The process remains quite similar to sorting the list by exchanging data, however, in this case, we have an additional variable r
that will always correspond to the node previous than the p
node.
Let's take a simple example of how we will swap two nodes by modifying links. Suppose we have a linked list with the following items:
And we want to swap 65 and 35. At this point in time p
corresponds to node 65, and q
corresponds to node 35. The variable r
will correspond to node 45 (previous to node p
). Now if the node p
is greater than node q
, which is the case here, the p.ref
will be set to q.ref
and q.ref
will be set to p
. Similarly, r.ref
will be set to q
. This will swap nodes 65 and 35.
The following method implements the bubble sorting for the linked list by modifying links:
Add the bub_sort_linkchange()
method to the LinkedList
class that you created in the last article.
Once you add the method to the linked list, create any set of nodes using the make_new_list()
function and then use the bub_sort_linkchange()
to sort the list. You should see the sorted list when you execute the traverse_list()
function.
In this section we will see how we can merge two sorted linked lists in a manner that the resulting linked list is also sorted. There are two approaches to achieve this. We can create a new linked list that contains individually sorted lists or we can simply change links of the two linked list to join the two sorted linked list. In the second case, we do not have to create a new linked list.
Let's first see how we can merge two linked lists by creating a new list.
Merging Sorted Linked Lists by Creating a New List
Let's first dry run the algorithm to see how we can merge two sorted linked list with the help of a new list.
Suppose we have the following two sorted linked lists:
list1:
list2:
These are the two lists we want to merge. The algorithm is straight forward. All we will need is three variables, p
, q
, and em
, and an empty list newlist
.
At the beginning of the algorithm, p
will point to the first element of the list1
whereas q
will point to the first element of the list2
. The variable em
will be empty. At the start of the algorithm, we will have the following values:
Next, we will compare the first element of the list1
with the first element of list2
, in other words, we will compare the values of p
and q
and the smaller value will be stored in the variable em
which will become the first node of the new list. The value of em
will be added to the end of the newlist
.
After the first comparison we will have the following values:
Since q
was less than p
, therefore, we store the value of q
in em
moved 'q' one index to the right. In the second pass, we will have the following values:
Here since p
was smaller, we add the value of p
to newlist
, and set em
to p
and then moved p
one index to the right. In the next iteration we have:
Similarly, in the next iteration:
And in the next iteration, p
will again be smaller than q
, hence:
Finally,
When one of the list becomes None
, all the elements of the second list are added at the end of the new list. Therefore, the final list will be:
The Python script for merging two sorted lists is as follows:
In the script above we have two methods: merge_helper()
and merge_by_newlist()
. The first method merge_helper()
takes a linked list as a parameter and then passes the self
class, which is a linked list itself and the linked list passed to it as a parameter, to the merge_by_newlist()
method.
The merge_by_newlist()
method merges the two linked by creating a new linked list and returns the start node of the new linked list. Add these two methods to the LinkedList
class. Create two new linked lists, sort them using the bub_sort_datachange()
or the bub_sort_linkchange()
methods that you created in the last section and then use the merge_by_newlist()
to see if you can merge two sorted linked lists or not.
Merging Sorted Linked Lists by Rearranging Links
In this approach, a new linked list is not used to store the merger of two sorted linked lists. Rather, the links of the two linked lists are modified in such a way that two linked lists are merged in a sorted manner.
Let's see a simple example of how we can do this. Suppose we have the same two lists list1
and list2
:
list1:
list2:
We want to merge them in a sorted manner by rearranging the links. To do so we need variables p
, q
and em
. Initially, they will have the following values:
Next, we will compare the first element of the list1
with the first element of list2
, in other words, we will compare the values of p
and q
and the smaller value will be stored in the variable em
which will become the first node of the new list.
After the first comparison we will have the following values:
After the first iteration, since q
is less than p
, the start node will point towards q
and q
will become q.ref
. The em
will be equal to start. The em
will always refer to the newly inserted node in the merged list.
Here since p
was smaller than the q
, the variable em
now points towards the original value of p
and p
becomes p.ref
.
Here since q
was smaller than p
, em
points towards q
and q
becomes q.ref
.
Similarly em
here points towards q
.
And here em
points towards becomes p
.
When one of the lists becomes None
, the elements from the second list are simply added at the end.
The script that contains functions for merging two lists without creating a new list is as follows:
In the script above we have two methods: merge_helper2()
and merge_by_linkChange()
. The first method merge_helper2()
takes a linked list as a parameter and then passes the self class which is a linked list itself and the linked list passed to it as a parameter, to the merge_by_linkChange()
, which merges the two linked by modifying the links and returns the start node of the merged list. Add these two methods to the LinkedList
class. Create two new linked lists, sort them using the bub_sort_datachange()
or the bub_sort_linkchange()
methods that you created in the last section and then use the merge_by_newlist()
to see if you can merge two sorted linked lists or not. Let's see this process in action.
Create a new linked list using the following script:
The script will ask you for the number of nodes to enter. Enter as many nodes as you like and then add values for each node as shown below:
Next, create another linked list repeating the above process:
Next, add a few dummy nodes with the help of the following script:
The next step is to sort both the lists. Execute the following script:
Finally, the following script merges the two linked lists:
To see if the lists have actually been merged, execute the following script:
The output looks like this:
In this article, we continued from where we left in the . We saw how we can sort merge lists by changing data and then my modifying links. Finally, we also studied different ways of merging two sorted linked lists.