{"id":22295,"date":"2022-11-02T10:16:52","date_gmt":"2022-11-02T17:16:52","guid":{"rendered":"https:\/\/coderpad.io\/?p=22295"},"modified":"2023-06-05T13:56:35","modified_gmt":"2023-06-05T20:56:35","slug":"an-introduction-to-linked-list-data-structures","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/an-introduction-to-linked-list-data-structures\/","title":{"rendered":"An Introduction to Linked List Data Structures"},"content":{"rendered":"\n<p>Linked lists are one of the fundamental <a href=\"https:\/\/www.educative.io\/answers\/what-are-linear-data-structures\" target=\"_blank\" rel=\"noopener\">linear data structures<\/a> alongside stacks, queues, arrays, and lists.<\/p>\n\n\n\n<p>A linked list is a continuous list of nodes where a node is a block structure housing the node value and a pointer (or memory) address to the next node. Each node from the head node has a <code>next<\/code> pointer that keeps the address of the next node till it gets to the last node, which points to nothing.<\/p>\n\n\n\n<p>Linked lists are used in web browsers to keep track of web pages visited and moves in multi-player board games. Linked lists also find usage in implementing other linear data structures and graphs, dynamic memory allocation, and performing arithmetic operations on long integers.<\/p>\n\n\n\n<p>The connection from one node to another node differentiates linked lists from a regular list or array. Unlike the linked lists, arrays don&#8217;t keep track of their next or other values.<\/p>\n\n\n\n<p>Here is a visual representation of a linked list:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63626e083150c.png\" alt=\"A linked list with 4 nodes. Each node points to another node.\nThe last node always points to NULL, except for circular linked lists.\"\/><figcaption class=\"wp-element-caption\">A linked list with 4 nodes. Each node points to another node.The last node always points to NULL, except for circular linked lists.<\/figcaption><\/figure>\n\n\n\n<p><strong style=\"color: revert; font-size: revert; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\">Types of Linked Lists<\/strong><\/p>\n\n\n\n<p><em>The last node always points to NULL, except for circular linked lists.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Types of Linked Lists<\/strong><\/h2>\n\n\n\n<p>There are three types of linked lists:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Singly-linked list<\/li>\n\n\n\n<li>Doubly linked list<\/li>\n\n\n\n<li>Circular linked list<\/li>\n<\/ol>\n\n\n\n<p>You can derive other linked lists from the basic ones, e.g., circular doubly linked lists. This article teaches you how to implement the singly and doubly linked list and briefly discusses the circular linked list.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 In this article, we\u2019ll use the Python programming language for the implementations.<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Singly-linked list<\/h2>\n\n\n\n<p>You\u2019re tasked with building a tool to retrieve documents connected to each other <strong>but<\/strong> stored at random memory locations. You\u2019re puzzled as to what data structure to use to fit this use case, as arrays store documents sequentially. The good news is that you can use the singly linked list for this task.<\/p>\n\n\n\n<p>A singly linked list is defined by its node. A singly linked list node has a value and the next pointer. The diagram below is an example of a singly linked list.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63626e083150c.png\" alt=\"A linked list with 4 nodes. Each node points to another node.\nThe last node always points to NULL, except for circular linked lists.\"\/><figcaption class=\"wp-element-caption\">A linked list with 4 nodes. Each node points to another node. The last node always points to NULL, except for circular linked lists.<\/figcaption><\/figure>\n\n\n\n<p>To implement the linked list, start with the node class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Node<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, value)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        self.value = value\n        self.next = <span class=\"hljs-literal\">None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the node class comprises two attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>: The value to be stored in the node.<\/li>\n\n\n\n<li><code>next<\/code>: The pointer address to the next node. It has a default value of None ( NULL ).<\/li>\n<\/ul>\n\n\n\n<p>Next, you need to implement the linked list class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">The linked list class<\/h3>\n\n\n\n<p>The linked list class is a wrapper for adding methods for node operations, such as appending a node, prepending a node, and deleting a node. Let\u2019s say you want to append a node to the middle of a long chain of nodes. Doing so manually would look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">nodes = Node(<span class=\"hljs-number\">10<\/span>)\nnodes.next = Node(<span class=\"hljs-number\">11<\/span>)\nnodes.next.next = Node(<span class=\"hljs-number\">13<\/span>)\nnodes.next.next.next = Node(<span class=\"hljs-number\">14<\/span>)\n\n<span class=\"hljs-comment\"># Insert a new node after 11.<\/span>\n\nnew_node = Node(<span class=\"hljs-number\">12<\/span>)\nnew_node.next = nodes.next.next\nnodes.next.next = new_node<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The process above is stressful. In a scenario where you need to perform this same operation for a couple of nodes, say 10, you have to do this manually every time. This operation brings forth unnecessary code bulkiness and complexity &#8211; known as space complexity. Having a linked list class with a method solves this all.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f Each new variable defined in a program takes a significant amount of space. For example, a node costs 48 bytes of space, and a thousand nodes will cost 48000 bytes. Each time the program is executed, this amount of space is used for a single node initialization operation ignoring the internal space used by the linked list methods. Not only does this extra space slow down your application, but it also impacts the planet via poor <a href=\"https:\/\/coderpad.io\/blog\/engineering-management\/the-environmental-impact-of-digital-technology-and-how-software-eco-design-can-help\/\">eco-design<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p>The linked list class is independent. The <code>LinkedList<\/code> class implementation is:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">LinkedList<\/span>:<\/span> \n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span> \n        self.head = <span class=\"hljs-literal\">None<\/span>\n        self.tail = <span class=\"hljs-literal\">None<\/span>\n        Self.size = <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the linked list class comprises three attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>head<\/code>: The head of the linked list, which is the first node.<\/li>\n\n\n\n<li><code>tail<\/code>: The tail of the linked list, which is the last node.<\/li>\n\n\n\n<li><code>size<\/code>: The size of the linked list.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Singly-linked list operations<\/h3>\n\n\n\n<p>The primary operations of a singly linked list include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Traversal<\/li>\n\n\n\n<li>Insertion<\/li>\n\n\n\n<li>Deletion<\/li>\n<\/ol>\n\n\n\n<p>To perform the operations mentioned above, you need to define the methods under the linked list class.&nbsp;<\/p>\n\n\n\n<p>Before you begin to implement the primary operations, however, you will need to implement the <code>__len__<\/code> method in the <code>LinkedList<\/code> class to return the length of your linked list stored in the <code>self.size<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">def __len__(<span class=\"hljs-keyword\">self<\/span>) -&gt; int:\n   <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">self<\/span>.size<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With the <code>__len__<\/code> method implemented, you can return the size of the linked list with <code>len(linkedlist)<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Traversal<\/h4>\n\n\n\n<p>In simple words, traversal is a process of accessing elements stored in an object. A traversal operation is executed to print the elements in a linked list.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s implement our first method, <code>__str__<\/code>, to print out the elements in the linked list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__str__<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; str:<\/span>\n    linkedlist = <span class=\"hljs-string\">\"\"<\/span>\n    current = self.head\n \n    <span class=\"hljs-keyword\">while<\/span> current <span class=\"hljs-keyword\">and<\/span> current.next <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\n        linkedlist += str(current.value) + <span class=\"hljs-string\">\"-&gt;\"<\/span>\n        current = current.next\n \n    linkedlist += str(current.value)\n    <span class=\"hljs-keyword\">return<\/span> linkedlist<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>__str__<\/code> method in Python returns the string representation of an object. This method returns the node values in your linked list as a string separated by <code>-&gt;<\/code>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Insertion<\/h4>\n\n\n\n<p>The insertion operation enables you to add nodes to your linked list. You can add nodes to your linked list by prepending the node, appending the node, or by adding nodes at specific positions in the linked list.<\/p>\n\n\n\n<p><strong>Prepending a node<\/strong><\/p>\n\n\n\n<p>The prepend method adds a new node at the head of the linked list, making it the new head node. The previous head node is automatically set as the next node after the new node.&nbsp;<\/p>\n\n\n\n<p>Implement the prepend method in the <code>LinkedList<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">prepend<\/span><span class=\"hljs-params\">(self, element)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n    new_node = Node(element)\n    new_node.next = self.head\n\n    self.head = new_node\n \n    <span class=\"hljs-keyword\">if<\/span> self.tail == <span class=\"hljs-literal\">None<\/span>:\n        self.tail = self.head\n \n    self.size += <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the <code>prepend<\/code> method takes an argument <code>element<\/code>, which is, in turn, passed to the <code>Node<\/code> class to form a node <code>new_node<\/code>. The <code>new_node<\/code> has its next pointer set to the head of the linked list <code>self.head<\/code>, and the linked list\u2019s head node also points to the new node created.&nbsp;<\/p>\n\n\n\n<p>The method then checks if the tail of the linked list does not exist; if the tail doesn\u2019t exist, it is set to the value of the head node.<\/p>\n\n\n\n<p>Lastly, the method increments the size of the linked list.<\/p>\n\n\n\n<p><strong>Appending a node<\/strong><\/p>\n\n\n\n<p>The <code>append<\/code> method adds a new node to the end of the linked list. This method traverses the linked list until the next node pointer points to <code>None<\/code> &#8211; signifying the end of the linked list. The new node is attached to the tail\u2019s node pointer, and the size of the linked list is incremented.<\/p>\n\n\n\n<p>Implement the method in the linked list class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">append<\/span><span class=\"hljs-params\">(self, element)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n    node = self.head\n    <span class=\"hljs-keyword\">while<\/span> node.next != <span class=\"hljs-literal\">None<\/span>:\n        node = node.next\n \n    node.next = Node(element)\n    self.tail = node.next\n    self.size += <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><strong>Appending at a specific position<\/strong><\/p>\n\n\n\n<p>The last insertion operation is to append at a specific position. In this operation, a new node is added to the position specified. For example, if the linked list has four values, you can insert a new node at the third position. The node in the second position will have its next pointer pointing at the new node, and the new node points at the node originally in the third position.<\/p>\n\n\n\n<p>In the <code>LinkedList<\/code> class, add the method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">add_at_position<\/span><span class=\"hljs-params\">(self, position, element)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> position &lt; <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">or<\/span> position &gt; self.size:\n        <span class=\"hljs-keyword\">raise<\/span> IndexError(<span class=\"hljs-string\">\"Position is invalid\"<\/span>)\n\n    <span class=\"hljs-keyword\">if<\/span> position == <span class=\"hljs-number\">1<\/span>:\n        self.prepend(element)\n        <span class=\"hljs-keyword\">return<\/span>\n\n    index = <span class=\"hljs-number\">1<\/span>\n    prev = <span class=\"hljs-literal\">None<\/span>\n    current = self.head\n    new_node = Node(element)\n\n    <span class=\"hljs-keyword\">while<\/span> index != position:\n        index += <span class=\"hljs-number\">1<\/span>\n        prev = current\n        current = current.next\n    prev.next = new_node\n    new_node.next = current\n    self.size += <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With the insertion methods in place, create a new linked list and perform some operations:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">ll = LinkedList()\nll.prepend(<span class=\"hljs-number\">40<\/span>)\nll.append(<span class=\"hljs-number\">50<\/span>)\nll.add_at_position(<span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">45<\/span>)\n \nprint(len(ll)) <span class=\"hljs-comment\"># 3<\/span>\nprint(ll) <span class=\"hljs-comment\"># 40 -&gt; 45 -&gt; 50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Deletion<\/h4>\n\n\n\n<p>The deletion operation involves removing a node from the linked list either from the head, tail or at a specified position. Implement the method for this operation in the <code>LinkedList<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">delete_node<\/span><span class=\"hljs-params\">(self, position)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> position &lt; <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">or<\/span> position &gt; self.size:\n        <span class=\"hljs-keyword\">raise<\/span> IndexError(<span class=\"hljs-string\">\"Invalid position!\"<\/span>)\n \n    <span class=\"hljs-keyword\">if<\/span> position == <span class=\"hljs-number\">1<\/span>:\n        self.head = self.head.next\n        <span class=\"hljs-keyword\">return<\/span>\n \n    <span class=\"hljs-keyword\">if<\/span> position == self.size:\n        prev = <span class=\"hljs-literal\">None<\/span>\n        current = self.head\n \n        <span class=\"hljs-keyword\">while<\/span> current.next <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\n            prev = current\n            current = current.next\n \n        prev.next = <span class=\"hljs-literal\">None<\/span> \n        self.tail = prev\n \n        self.size -= <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">return<\/span>\n \n   <span class=\"hljs-comment\"># Position specified isn't head nor tail.<\/span>\n     index = <span class=\"hljs-number\">1<\/span>\n     prev = <span class=\"hljs-literal\">None<\/span>\n     current = self.head\n \n     <span class=\"hljs-keyword\">while<\/span> index != position:\n         index += <span class=\"hljs-number\">1<\/span>\n         prev = current\n         current = current.next\n \n     prev.next = current.next\n \n     self.size -= <span class=\"hljs-number\">1<\/span>\n \n     <span class=\"hljs-keyword\">return<\/span> current.element<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the <code>delete_node<\/code> function performs three delete operations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Deleting the head node: If the position specified is 1, the head node is deleted by setting the value of the head node to the second node in the linked list.<\/li>\n\n\n\n<li>Deleting the tail node: If the position specified equals the number of nodes present in the linked list, the linked list is traversed to the end, and the second to the last node\u2019s pointer is set to <code>None<\/code> to eliminate the tail node.<\/li>\n\n\n\n<li>Deleting a different node: If the position doesn\u2019t match the head or tail nodes, the linked list is traversed until the specified position is reached. The node preceding the node at the specified position will have its next pointer set to the node located after the deleted node.<\/li>\n<\/ol>\n\n\n\n<p>All of the operations above decrease the size of the linked list. An <code>IndexError<\/code> will be returned if a wrong position is passed to the method.<\/p>\n\n\n\n<p>If you delete the second node you created earlier, the new linked list will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">ll.delete_node(<span class=\"hljs-number\">2<\/span>)\nprint(ll, len(ll)) <span class=\"hljs-comment\"># Prints 40 -&gt; 50, 2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=233298&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Doubly linked list<\/h2>\n\n\n\n<p>You\u2019re tasked with implementing a new feature in a multiplayer board game (chess, checkers, tic-tac-toe, etc.) that enables a user to go back to their previous move. To implement this feature, you\u2019ll likely need to use a doubly linked list data structure.&nbsp;<\/p>\n\n\n\n<p>A doubly linked list has two node pointers\u2014one pointer pointing to the previous node and the other pointing to the next node.<\/p>\n\n\n\n<p>Here is a graphical representation:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63626e09aeb72.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A doubly linked list representation. Each node keeps a pointer to the previous and next node.<\/figcaption><\/figure>\n\n\n\n<p>To implement a doubly linked list, implement the node class first:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Node<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, value)<\/span>:<\/span>\n        self.value = value\n        self.previous = <span class=\"hljs-literal\">None<\/span>\n        self.next = <span class=\"hljs-literal\">None<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Node<\/code> class above comprises three attributes:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>: The value to be stored in the node.<\/li>\n\n\n\n<li><code>previous<\/code>: The pointer address of the previous node.<\/li>\n\n\n\n<li><code>next<\/code>: The pointer address of the next node.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">The linked list class<\/h3>\n\n\n\n<p>The linked list class is a wrapper for adding methods for node operations. The <code>LinkedList<\/code> class definition for a doubly linked list is:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">LinkedList<\/span>:<\/span> \n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span> \n        self.head = <span class=\"hljs-literal\">None<\/span>\n        self.tail = <span class=\"hljs-literal\">None<\/span>\n        self.size = <span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Doubly linked list operations<\/h3>\n\n\n\n<p>Just as before, the primary operations of a doubly linked list include:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Traversal<\/li>\n\n\n\n<li>Insertion<\/li>\n\n\n\n<li>Deletion<\/li>\n<\/ol>\n\n\n\n<p>To perform the above-mentioned operations, you need to define the methods under the linked list class.<\/p>\n\n\n\n<p>Before you begin to implement the primary operations, implement the <code>__len__<\/code> method in the <code>LinkedList<\/code> class to return the length of your linked list stored in the <code>self.size<\/code> variable:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__len__<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; int:<\/span>\n       <span class=\"hljs-keyword\">return<\/span> self.size<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With the <code>__len__<\/code> method implemented, you can return the size of the linked list with <code>len(linkedlist)<\/code>.<\/p>\n\n\n\n<p>This should look familiar, because it is the exact same method definition as a singly linked list.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Traversal<\/h4>\n\n\n\n<p>Let\u2019s implement our first method, <code>__str__<\/code>, to print out the elements in the linked list:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__str__<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; str:<\/span>\n    linkedlist = <span class=\"hljs-string\">\"\"<\/span>\n    current = self.head\n \n    <span class=\"hljs-keyword\">while<\/span> current <span class=\"hljs-keyword\">and<\/span> current.next <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\n        linkedlist += str(current.value) + <span class=\"hljs-string\">\"&lt;-&gt;\"<\/span>\n        current = current.next\n \n    linkedlist += str(current.value)\n    <span class=\"hljs-keyword\">return<\/span> linkedlist<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the <code>__str__<\/code> method will iterate through the linked list and store each node value separated by the delimiter <code>&lt;-&gt;<\/code> in a variable <code>linkedlist<\/code> which is returned when a linked list object is to be printed.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f The <code>__str__<\/code> and <code>__len__<\/code> methods are the same for all linked lists. The only difference is the delimiter <code>-<\/code>&gt; for singly and&nbsp; <code>&lt;-&gt;<\/code> for the doubly linked list.<\/p>\n<\/blockquote>\n\n\n\n<h4 class=\"wp-block-heading\">Insertion<\/h4>\n\n\n\n<p>The insertion operation enables you to add nodes to your linked list either by prepending the node, appending the node, or adding the node at specific positions in the linked list.<\/p>\n\n\n\n<p><strong>Prepending a node<\/strong><\/p>\n\n\n\n<p>The <code>prepend<\/code> method adds a new node at the head of the linked list. Implement the prepend method in the <code>LinkedList<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">prepend<\/span><span class=\"hljs-params\">(self, element)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n    new_node = Node(element)\n    new_node.next = self.head\n \n    <span class=\"hljs-keyword\">if<\/span> self.head <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-keyword\">not<\/span> <span class=\"hljs-literal\">None<\/span>:\n        self.head.previous = new_node\n \n    self.head = new_node\n \n    <span class=\"hljs-keyword\">if<\/span> self.tail == <span class=\"hljs-literal\">None<\/span>:\n        self.tail = self.head\n \n    self.size += <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the method starts by creating a new node, <code>new_node<\/code>, with the argument passed to the <code>prepend<\/code> method. The method assigns the next pointer to the head node. It then checks if the linked list is not empty and assigns the head node\u2019s previous pointer to the new node.<\/p>\n\n\n\n<p>Once the pointer assignments are complete, the new node is assigned as the new head of the linked list.<\/p>\n\n\n\n<p><strong>Appending a node<\/strong><\/p>\n\n\n\n<p>The <code>append<\/code> method adds a new node to the end of the linked list. This method traverses the linked list until the next node pointer points to <code>None<\/code> (the end of the linked list). Once the end of the linked list is reached, the new node is attached to the tail\u2019s node pointer, and the size of the linked list is incremented.<\/p>\n\n\n\n<p>Implement the method in the linked list class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">append<\/span><span class=\"hljs-params\">(self, element)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> self.tail <span class=\"hljs-keyword\">is<\/span> <span class=\"hljs-literal\">None<\/span>:\n        self.prepend(element)\n        <span class=\"hljs-keyword\">return<\/span>\n \n    new_node = Node(element)\n \n    self.tail.next = new_node\n \n    new_node.previous = self.tail\n \n    self.tail = new_node\n \n    self.size += <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the <code>append<\/code> method checks if the tail of the linked list is <code>None<\/code>. If the tail is <code>None<\/code>, the method performs a prepend operation and exits.<\/p>\n\n\n\n<p>However, if the tail exists, a new node is created and its previous pointer is set to the linked list tail node. The tail node as well sets its next pointer to the new node. Once the pointer assignment is completed, the new node is made the new tail by assigning the node to the <code>self.tail<\/code> variable.<\/p>\n\n\n\n<p><strong>Appending at a specific position<\/strong><\/p>\n\n\n\n<p>Linked list nodes can be appended at a specified position. An example is inserting a new node at the fourth position in a linked list with five values.<\/p>\n\n\n\n<p>The node before the position specified will have its next pointer pointing at the new node and its next pointer pointing at the node originally at the position specified.<\/p>\n\n\n\n<p>In the <code>LinkedList<\/code> class, add the method:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">add_at_position<\/span><span class=\"hljs-params\">(self, position, element)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> position &lt; <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">or<\/span> position &gt; self.size:\n        <span class=\"hljs-keyword\">raise<\/span> IndexError(<span class=\"hljs-string\">\"Position is invalid\"<\/span>)\n    <span class=\"hljs-keyword\">if<\/span> position == <span class=\"hljs-number\">1<\/span>:\n        self.prepend(element)\n        <span class=\"hljs-keyword\">return<\/span>\n \n    index = <span class=\"hljs-number\">1<\/span>\n    current = self.head\n    new_node = Node(element)\n \n    <span class=\"hljs-keyword\">while<\/span> index != position:\n        index += <span class=\"hljs-number\">1<\/span>\n        current = current.next\n \n    prev = current.previous\n \n    prev.next = new_node\n    new_node.previous = prev\n    new_node.next = current\n    current.previous = new_node\n \n    self.size += <span class=\"hljs-number\">1<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the method first checks if the position is valid before proceeding. If the position is invalid, the method returns an <code>IndexError<\/code>.<\/p>\n\n\n\n<p>If the position specified is the head node position <strong>1, <\/strong>the method invokes the <code>prepend<\/code> method for the insertion. Otherwise, the method traverses the whole linked list until it gets to the position and performs the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The method assigns the preceding node\u2019s next pointer to the new node<\/li>\n\n\n\n<li>Sets the new node\u2019s previous pointer to the preceding node<\/li>\n\n\n\n<li>Sets the new node\u2019s next pointer to the current node<\/li>\n\n\n\n<li>Sets the current node\u2019s previous pointer to the new node<\/li>\n<\/ul>\n\n\n\n<p>With the insertion methods in place, create a new linked list and perform some operations:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">dll = LinkedList()\ndll.prepend(<span class=\"hljs-number\">40<\/span>)\ndll.prepend(<span class=\"hljs-number\">30<\/span>)\ndll.append(<span class=\"hljs-number\">50<\/span>)\ndll.add_at_position(<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">10<\/span>)\n \nprint(dll) <span class=\"hljs-comment\"># 10&lt;-&gt;30&lt;-&gt;40&lt;-&gt;50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Deletion<\/h4>\n\n\n\n<p>The deletion operation removes a node from the linked list either from the head, tail or at a specified position. Implement the method for this operation in the <code>LinkedList<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">delete_node<\/span><span class=\"hljs-params\">(self, position)<\/span>:<\/span>\n    <span class=\"hljs-keyword\">if<\/span> position &lt; <span class=\"hljs-number\">1<\/span> <span class=\"hljs-keyword\">or<\/span> position &gt; self.size:\n        <span class=\"hljs-keyword\">raise<\/span> IndexError(<span class=\"hljs-string\">\"Invalid position!\"<\/span>)\n\n    <span class=\"hljs-keyword\">if<\/span> position == <span class=\"hljs-number\">1<\/span>:\n        node_to_remove = self.head\n        newHead = self.head.next\n        node_to_remove.next = <span class=\"hljs-literal\">None<\/span>\n        node_to_remove.previous = <span class=\"hljs-literal\">None<\/span>\n\n        self.head = newHead\n        self.size -= <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">return<\/span> node_to_remove.value\n\n    <span class=\"hljs-keyword\">if<\/span> position == self.size:\n        node_to_remove = self.tail\n        new_tail = self.tail.previous\n\n        node_to_remove.previous = <span class=\"hljs-literal\">None<\/span>\n        new_tail.next = <span class=\"hljs-literal\">None<\/span>\n\n        self.tail = new_tail\n        self.size -= <span class=\"hljs-number\">1<\/span>\n        <span class=\"hljs-keyword\">return<\/span> node_to_remove.value\n    index = <span class=\"hljs-number\">1<\/span>\n    current = self.head\n\n    <span class=\"hljs-keyword\">while<\/span> index != position:\n        index += <span class=\"hljs-number\">1<\/span>\n        current = current.next\n\n    prev = current.previous\n    prev.next = current.next\n    current.next.previous = prev\n    self.size -= <span class=\"hljs-number\">1<\/span>\n    <span class=\"hljs-keyword\">return<\/span> current.value<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the <code>delete_node<\/code> function performs three delete operations:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Deleting the head node: If the position specified is 1, the head node is deleted by setting both pointers to <code>None<\/code>, and the second node is assigned the head node.<\/li>\n\n\n\n<li>Deleting the tail node: If the position specified equals the number of nodes present in the linked list, the linked list is traversed to the end, and the second to the last node\u2019s pointer is set to <code>None<\/code> to eliminate the tail node. The outgoing tail node\u2019s pointers are also set to <code>None<\/code> respectively.<\/li>\n\n\n\n<li>Deleting a different node: If the position doesn\u2019t match the head or tail nodes, the linked list is traversed until the specified position is reached. For example, to delete the third node in a 5-value linked list, the second node will have its next pointer pointing to the fourth node, and the fourth node\u2019s previous pointer will now point to the second node.The node preceding the node at the position will have its next pointer set to the node of the next pointer of the node to be deleted. The next pointer after the node to be deleted will have its previous pointer pointing to the node preceding the current node to be deleted.<\/li>\n<\/ol>\n\n\n\n<p>All of the operations above decrease the size of the linked list. An <code>IndexError<\/code> will be returned if a wrong position is passed to the method.<\/p>\n\n\n\n<p>If you delete the second node you created earlier, the new linked list will be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-wrap-lines\">dll.delete_node(<span class=\"hljs-number\">2<\/span>)\n \nprint(dll) <span class=\"hljs-comment\"># 10&lt;-&gt;40&lt;-&gt;50<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=233307&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Circular linked lists<\/h2>\n\n\n\n<p>Circular linked lists are singly linked lists except that their last node points to the first node as opposed to None. As a result, the circular linked list does not have a tail node. Circular linked lists are commonly used to manage computing resources.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63626e0a784f4.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The circular linked list\u2019s last node\u2019s next pointer is set to the first node\u2019s address.<\/figcaption><\/figure>\n\n\n\n<p>The circular linked list shares the same logic for insertion, traversal, and deletion as the singly linked list with the only difference being the <strong>assignment of the last node\u2019s next pointer to the head node<\/strong>.<\/p>\n\n\n\n<p>For example, the append method for the circular linked list is:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">\ndef prepend(<span class=\"hljs-keyword\">self<\/span>, value):\n    cur = <span class=\"hljs-keyword\">self<\/span>.head\n    new_node = Node(value)\n    new_node.next = <span class=\"hljs-keyword\">self<\/span>.head\n\n    <span class=\"hljs-keyword\">if<\/span> not <span class=\"hljs-keyword\">self<\/span>.head:\n        new_node.next = new_node\n    <span class=\"hljs-keyword\">else<\/span>:\n        <span class=\"hljs-keyword\">while<\/span> cur.next != <span class=\"hljs-keyword\">self<\/span>.head:\n            cur = cur.next\n        cur.next = new_node\n    <span class=\"hljs-keyword\">self<\/span>.head = new_node<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the <code>prepend<\/code> method first checks if the head node is not present in the linked list. If there\u2019s no head node, the new node\u2019s next pointer is set to itself, creating a circle.<br><br>The circular linked list is implemented in the sandbox below to avoid repeating the implementation of the singular linked list.<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=233599&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Try it out in the CoderPad sandbox!<\/h2>\n\n\n\n<p>In this article, you have learned what linked lists are and how to implement singly and doubly linked lists. You can play with the sandboxes beneath this section to test the method you learned as well as implement new methods &#8211; the <a href=\"https:\/\/app.coderpad.io\">Coderpad Sandbox<\/a> is an interactive sandbox for peer coding, live coding, and interviews.<\/p>\n\n\n\n<p>Want more challenges? Create a sandbox and implement the reversal method for singly and doubly linked lists, create a tweet with your sandbox link, and tag <a href=\"https:\/\/twitter.com\/coderPad\" target=\"_blank\" rel=\"noopener\">CoderPad<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Linked lists are fundamental linear data structures alongside stacks, queues, arrays, and lists. Linked lists are used in web browsers, arithmetic operations, and board game implementation. Learn the linked list data structure and how to implement them in this blog post.<\/p>\n","protected":false},"author":1,"featured_media":22352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[37],"keyword-cluster":[],"class_list":["post-22295","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/22295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=22295"}],"version-history":[{"count":55,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/22295\/revisions"}],"predecessor-version":[{"id":32639,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/22295\/revisions\/32639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/22352"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=22295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=22295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=22295"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=22295"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=22295"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=22295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}