<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pedro Soares on Medium]]></title>
        <description><![CDATA[Stories by Pedro Soares on Medium]]></description>
        <link>https://medium.com/@hs_pedro?source=rss-d692b5089d9b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IERIYmOv4g1uXWbzTsmPkQ.jpeg</url>
            <title>Stories by Pedro Soares on Medium</title>
            <link>https://medium.com/@hs_pedro?source=rss-d692b5089d9b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:35:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hs_pedro/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Implementing a Heap in Python]]></title>
            <link>https://medium.com/@hs_pedro/implementing-a-heap-in-python-1036e759e0eb?source=rss-d692b5089d9b------2</link>
            <guid isPermaLink="false">https://medium.com/p/1036e759e0eb</guid>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[heap]]></category>
            <category><![CDATA[heapsort]]></category>
            <dc:creator><![CDATA[Pedro Soares]]></dc:creator>
            <pubDate>Mon, 20 Dec 2021 00:55:26 GMT</pubDate>
            <atom:updated>2021-12-20T12:24:21.937Z</atom:updated>
            <content:encoded><![CDATA[<p>Heap is an elegant data structure that is commonly used for Priority Queue implementation.</p><blockquote>Priority Queue is an abstract data structure, defines the behavior and interface, whereas Heap is a concrete data structure defining how it is implemented.</blockquote><p>An implementation of a priority queue generally provides the following methods:</p><ul><li><em>Insert(H, x)</em>: Given item <em>x</em>, insert into priority queue <em>H</em></li><li><em>Find(H)</em>:<em> </em>returns the element with the highest priority in queue <em>H</em>. Priority is commonly defined either by the <em>largest</em> or <em>smallest </em>key<em> </em>value among elements</li><li><em>Delete(H)</em>: remove the element with the <em>smallest </em>(or<em> largest</em>)<em> </em>value in the priority queue <em>H</em></li></ul><p>In this article, I will focus on Binary-Heap implementation, which means that a node can have at most two children. In a min-heap, a node dominates its children by having a smaller key than they do, while in a max-heap parent nodes dominate by being bigger.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*erSa5UTKpsPHPf25-eND0g.png" /><figcaption><a href="https://en.wikipedia.org/wiki/Heap_(data_structure)">Source: Wikipedia’s heap article</a></figcaption></figure><p>The data can be represented in an array of keys, in which the position of the left and right children can be deduced with a simple mathematical relationship. Given an element of index <em>k</em>:</p><ul><li><strong>left child index:</strong> 2*<em>k + </em>1</li><li><strong>right child index: </strong>2*<em>k</em> + 2</li><li><strong>parent index</strong>: <em>k </em>/2 (taking the floor)</li></ul><p>Heaps work by maintaining a partial order on the set of elements that is weaker than the sorted order (so it can be eﬃcient to maintain) yet stronger than random order (so the minimum element can be quickly identiﬁed).</p><h4>Applications and Usages</h4><p>We model several processes in our daily-basis with a priority queue, an example would be patients waiting in line at a hospital, where the severity of the injury could be used as the priority rule. Another great example is given by Skiena:</p><blockquote>Dating is the process of extracting the most desirable person from the data structure (Find-Maximum), spending an evening to evaluate them better, and then reinserting them into the priority queue with a possibly revised score.</blockquote><p>Other than that, they are widely used in other algorithms such as:</p><ul><li><strong>Sorting</strong>: <em>heapsort</em> has <em>O(n*logn) </em>time complexity at worst</li><li><strong>Greedy Graph Search — A* and Djikstra</strong>: greedy algorithms, such as the Djikstra’s shortest path, can use heaps to store priority-node pairs</li><li><strong>Huffman Coding</strong>: heaps can be used to store and retrieve the two lowest-frequency trees</li></ul><p><strong>Bonus</strong>: There is a well-known Facebook phone interview question that can be solved with a Min-Heap. The question was reported by the <a href="https://www.youtube.com/watch?v=pQfagNu3p54"><em>Keep on Coding </em>channel</a>:</p><blockquote>Given a list of flight start and end times, determine the maximum amount of airplanes in the air at the same time.<br><strong>Example Input</strong>:<br>Start: 2, End: 5<br>Start: 3, End: 7<br>Start: 8, End: 9</blockquote><blockquote><strong>Return</strong>: 2</blockquote><h4>Python’s stdlib Implementation</h4><p>On Python’s standard library, the API for heaps can be found in <a href="https://docs.python.org/3/library/heapq.html">heapq</a> module. For instance, one can heapify an array (<em>min-heap</em>, in this case<em>) </em>by doing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2973613fedd4b9bc7a48670dc80d3eba/href">https://medium.com/media/2973613fedd4b9bc7a48670dc80d3eba/href</a></iframe><h4>Custom Implementation</h4><p>The implementation of <strong>MinHeap</strong> consists of defining an internal <em>list</em>, storing the elements, and implementation of the following methods:</p><ul><li><em>peek</em> (or find-minimum): returns the smallest key stored in constant time</li><li><em>add</em>: insert an element in the heap in its appropriate location</li><li><em>poll </em>(or extract-min): removes the smallest key stored in the heap</li></ul><p>In the end, I will implement a <em>heapsort</em> function which will become very straightforward once we define our <em>MinHeap</em> class.</p><h4>Helper Methods</h4><p>The class implementation will start with some helpers implementing the basic mathematical relationship among parents, right and left children. Those methods will help in getting a given child or parent and accessing it.</p><p>Also, we can define them as private, even though private encapsulation in Python does not go beyond <a href="https://docs.python.org/3/tutorial/classes.html#private-variables">name mangling</a>, I will keep them private for semantic purposes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8f2ba12036e5a96a813ec7aba2bba46/href">https://medium.com/media/f8f2ba12036e5a96a813ec7aba2bba46/href</a></iframe><p>We will change the __init__ to receive a list of elements once we implement the insertion methods.</p><h4>Inserting into the MinHeap</h4><p>Inserting a key into a heap is always a 2-step approach. First, we want to add the incoming key into the array as fast as possible, which means appending to the end. Then, we want to <em>heapify-up</em> (or <em>bubble up</em>) this newly-added element to its correct position.</p><p>The process of moving the new element to its appropriate position is going to be defined by the <em>heapify-up</em> function, which will recursively execute the following:</p><ol><li>Get the element (starting with the last one)</li><li>Check if it disobeys the priority rule, in this case (min-heap), check if its key value is <strong>smaller</strong> than the parent</li><li>If smaller, then swap with its parent</li><li>Recurse in the index of the parent</li><li>Keep bubbling up until reaching the root</li></ol><p>I will also define a helper method for swapping elements, this will be done constantly in heap operations.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f9b006e95beac4a7168a40f6cce2580/href">https://medium.com/media/0f9b006e95beac4a7168a40f6cce2580/href</a></iframe><p>Now, the __init__ can receive a list of elements and add them to the heap.</p><h4><strong>Deleting from the MinHeap</strong></h4><p>Similar to the insertion, deleting from the heap is a 2-step process consisting of first removing the highest-priority key, then reorganizing the heap. The first process is simply removing the first element of the array, which is tricky since for large arrays it involves a big memory shift, thus complexity can go up to <em>O(n)</em>. Due to that, we do not simply remove the first one, what we actually do is copy the last element to the first position and then shrink the size of the array by 1.</p><p>After moving the last element to the first position and shrinking, we proceed to <em>heapify-down</em> (or <em>bubble-down</em>) the first element to its correct position in the heap. This is done recursively by executing the following steps:</p><ol><li>Get the element key (starts with the first element)</li><li>Check if the key is greater than any of the children (left or right)</li><li>If greater, than it is in the wrong position, swap with the smallest between left and right</li><li>Continue to heapify down with the smallest child index</li><li>Repeat until no more children</li></ol><p>The <em>heapify down</em> process takes <em>O(log n)</em> time complexity since as we navigate through the heap we always split in half the number of paths — by choosing one of the children.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0ad8dafdf8d61c982924fbbe72d3a4f2/href">https://medium.com/media/0ad8dafdf8d61c982924fbbe72d3a4f2/href</a></iframe><h4><strong>Peek</strong></h4><p>The <em>peek</em> operation needs to return the smallest key from the heap, in our case, this is the first element of the heap — given in constant time.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9efee6cd159795bf32dbc218f1a49987/href">https://medium.com/media/9efee6cd159795bf32dbc218f1a49987/href</a></iframe><h4>Heapsort</h4><p>Sorting with heaps then becomes very straightforward, first, we add the unsorted elements to the heap and keep polling. In each iteration of the polling loop, we are extracting the smallest key and re-organizing the heap.</p><p>The time complexity of this operation is <em>O(n*log n)</em>, since each time for each element that we want to sort we need to <em>heapify down</em>, after polling.</p><p><strong>Advantages</strong></p><ul><li><em>O(n * log n)</em> time complexity in the worst case</li><li>In-place algorithm</li></ul><p><strong>Disadvantages</strong></p><ul><li>Not stable</li><li>Very difficult to parallelize compared to other algorithms such as <a href="https://en.wikipedia.org/wiki/Merge_sort">Merge Sort</a> and <a href="https://en.wikipedia.org/wiki/Quicksort">Quick Sort</a></li></ul><p><strong>Using auxiliary space O(n)</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b86c4cb2ba1d305469f401c45c82dc3c/href">https://medium.com/media/b86c4cb2ba1d305469f401c45c82dc3c/href</a></iframe><p><strong>In-place</strong></p><p>For the in-place variation, we want to heapify the children so that the left child is always smaller than the right child. By doing that, we enforce that the array will be sorted at the end — this operation will take <em>O(n) </em>time complexity and still makes our algorithm <em>O(n * log n)</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d6df6d899c75c531420c1b947eb9b11/href">https://medium.com/media/9d6df6d899c75c531420c1b947eb9b11/href</a></iframe><h4>Additional Resources</h4><p>The complete code can be found here: <a href="https://gist.github.com/hspedro/3448a4602ab727ad7d015e9d3e5cd471">https://gist.github.com/hspedro/3448a4602ab727ad7d015e9d3e5cd471</a></p><p>This code helped me through multiple code interviews, and also to revisit some basic data structures fundamentals. I <strong>strongly</strong> recommend checking on these other resources!</p><ul><li><a href="https://www.algorist.com/">The Algorithm Design Manual — Chapter 3.5 and Chapter 4.3</a>, Steven Skiena</li><li><a href="https://docs.python.org/3/library/heapq.html">Official doc on <em>heapq</em></a>, Python</li><li><a href="https://realpython.com/python-heapq-module/#what-are-heaps">The Python heapq Module: Using Heaps and Priority Queues</a>, Real Python</li><li><a href="https://en.wikipedia.org/wiki/Heapsort">Heapsort</a>, Wikipedia</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1036e759e0eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing Linked Lists in Python]]></title>
            <link>https://medium.com/@hs_pedro/implementing-linked-lists-in-python-d881af444858?source=rss-d692b5089d9b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d881af444858</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-structures]]></category>
            <category><![CDATA[linked-lists]]></category>
            <category><![CDATA[lists-in-python]]></category>
            <dc:creator><![CDATA[Pedro Soares]]></dc:creator>
            <pubDate>Sun, 05 Dec 2021 19:47:34 GMT</pubDate>
            <atom:updated>2024-09-30T12:12:35.211Z</atom:updated>
            <content:encoded><![CDATA[<p>Generally, when studying data structures, we usually start with two types of common data structures: contiguous and linked. Each of them comes with its drawbacks, so it’s crucial to understand what is your problem before going deeper into the data structures.</p><p>The objective of this article is to help shine a light on how those structures are implemented in Python, what kind of drawbacks, and advantages they have, and even how you would implement some of them by yourself. Even though they all have implementations from Python’s standard library, getting to know how they work, might help you understand when and how to maximize its usage.</p><h4><strong>Python’s List a Contiguous Data Structure</strong></h4><p>Contiguously allocated structures are composed of single slabs of memory, some examples are Python’s lists, matrices, heaps, and hash tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*pDYgMgmtkIdo-aT94_6x9g.jpeg" /><figcaption>Credit: <a href="https://beginnersbook.com/2018/10/data-structure-array/">https://beginnersbook.com/2018/10/data-structure-array/</a></figcaption></figure><p><strong>Advantages:</strong></p><ul><li>Memory locality: physical continuity in memory can make use of highly optimized cache mechanisms</li><li>O(1) complexity to access given the index</li><li>Eﬃcient random access to items in arrays</li></ul><p><strong>Disadvantages:</strong></p><ul><li>Removing from the beginning is too costly due to resize</li><li>Space efficiency: Python always allocates a larger size than the elements it has, to avoid calling realloc each time a new element is appended to the list</li></ul><p><strong>Usages:</strong></p><ul><li><strong>Stacks</strong> (LIFO), good fit since we will always be inserting and removing the last element at a constant time</li><li><strong>Heaps</strong>, since each element is a node and the children can be accessed with a defined relationship. For example, in binary heaps the index of <strong>left_child</strong> is always given by <strong>2*parent_index + 1</strong> and <strong>right_child </strong>as <strong>2*parent_index + 2</strong></li><li><strong>Hash Tables</strong>, since an array-like list can be used to store the keys after computing the hash</li><li><strong>Depth-First Search (DFS) Algorithm</strong>, generally, implementing a DFS involves using a stack to store the nodes visited</li></ul><p><strong>Implementation in Python Standard Library:</strong></p><ul><li>Lists: <a href="https://docs.python.org/3/tutorial/datastructures.html#more-on-lists">https://docs.python.org/3/tutorial/datastructures.html#more-on-lists</a></li><li>CPython ref: <a href="https://github.com/python/cpython/blob/5c22476c01622f11b7745ee693f8b296a9d6a761/Include/listobject.h#L22">https://github.com/python/cpython/blob/5c22476c01622f11b7745ee693f8b296a9d6a761/Include/listobject.h#L22</a></li></ul><p>For a more in-depth analysis of how lists are implemented in CPython, check <a href="http://www.laurentluce.com/posts/python-list-implementation/">Laurent Luce’s article</a>.</p><h4>Linked Lists in Python</h4><p>A linked list is a type of linked data structure, which are structures composed of different chunks of memory that are linked by pointers. Some examples are trees, graphs (adjacency list), and linked lists.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/741/1*I3vCuSXrh9Gd-e4w341nlA.png" /><figcaption>Credit: <a href="https://slaystudy.com/difference-between-array-and-linked-list/">https://slaystudy.com/difference-between-array-and-linked-list/</a></figcaption></figure><p>Linked data structures have <strong>basic operations such as search, insert, and deletion</strong>. Also, they share some common properties:</p><ul><li>Each element contains a data field, which can have any type</li><li>Each element contains a pointer to another node</li></ul><p>Linked lists can be implemented as:</p><ul><li><strong>Singly-Linked List</strong>: each element points to one successor element, generally called <strong>next</strong></li><li><strong>Doubly-Linked List</strong>: each element points to one successor and one predecessor element</li><li><strong>Circular Linked List</strong>: first element points to the last element and the last element points to the first</li></ul><p><strong>Advantages:</strong></p><ul><li>O(1) to access the first element</li><li>Insertions put less pressure on memory since they involve only changing the address in memory that elements point to - avoiding resizing and shuffling</li><li>Deletion is more efficient than in lists since it does not involve reallocations and shuffles</li><li>Overﬂow on linked structures never occurs unless the memory is actually full</li><li>With large records, moving pointers is easier and faster than moving the<br>items themselves</li></ul><p><strong>Disadvantages</strong>:</p><ul><li>O(n) at the worst case for accessing</li><li>O(n) at the worst case for insertion</li><li>Always O(n) for searching, whereas for lists we can use binary search if ordered, which has O(log n)</li></ul><p><strong>Usages:</strong></p><ul><li><strong>Queues</strong> (FIFO), a good fit since we have constant time to remove the first element</li><li><strong>Breadth-First Search (BFS) Algorithm</strong>, generally, implementing a BFS involves using a queue to store the nodes visited</li><li><strong>Adjacency-List in Graphs</strong>, to store the connections of a vertice</li><li><strong>Hash Table</strong>, may use linked lists to store the chains of items that hash to the same position in the hash table</li></ul><p><strong>Implementation in Python Standard Library:</strong></p><ul><li>collections.deque (pronounced “deck”): <a href="https://docs.python.org/3/library/collections.html#collections.deque">https://docs.python.org/3/library/collections.html#collections.deque</a></li></ul><h3>Custom Implementation of a Singly Linked List</h3><p>Below you will find an example of how to implement a custom singly linked list in Python, this code helped me prepare for coding interviews. Having a good understanding of how it works, makes it easier to derivate a Doubly-Linked or Circular list implementation.</p><p>NOTE: The methods are named after <strong>collections.deque</strong> API, which supports:</p><ul><li><a href="https://docs.python.org/3/library/collections.html#collections.deque.append">append(x)</a>: Add <em>x</em> to the right side of the linked list</li><li><a href="https://docs.python.org/3/library/collections.html#collections.deque.appendleft">appendleft(x)</a>: Add <em>x</em> to the left side of the linked list</li><li><a href="https://docs.python.org/3/library/collections.html#collections.deque.pop">pop(x)</a>: Remove and return an element from the right side of the linked list</li><li><a href="https://docs.python.org/3/library/collections.html#collections.deque.popleft">popleft(x)</a>: Remove and return an element from the left side of the linked list</li></ul><h4>Node Class</h4><p>Pretty straightforward, as it is a Singly-Linked list, we only need to define a field for the <strong>next</strong>, pointing to the successor, and a field for <strong>data</strong>, which can receive any type of data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ce9466e70720b6d7f1c6d729d933d04/href">https://medium.com/media/3ce9466e70720b6d7f1c6d729d933d04/href</a></iframe><h4><strong>Singly-Linked List Class</strong></h4><p>The class can start with its constructor only creating the HEAD node, pointing to None , and may receive a list of nodes to pre-populate the list. In this implementation, I will leave the HEAD as an element with aNone value that will point to the first element added.</p><p><strong>__iter__(self)</strong></p><p>This method will be crucial to implement the other operations, keep in mind that it is not strictly necessary for search, append, pop, etc., however, I think it makes the code more Pythonic and uses Python’s <a href="https://docs.python.org/3/c-api/gen.html">generators</a>, which are much more memory-efficient.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0a861506f7cc4654eac1a94ed092d07/href">https://medium.com/media/d0a861506f7cc4654eac1a94ed092d07/href</a></iframe><p><strong>Queue operations</strong></p><p>Next, we can start implementing basic operations such as insert, remove, and search. For Queue, we generally want to <em>append</em> at the end and <em>remove </em>the first element added, those operations will be called <strong>append</strong> and <strong>popleft</strong> in the code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/faacd99959303e23f30621938b807437/href">https://medium.com/media/faacd99959303e23f30621938b807437/href</a></iframe><p>For convenience, we can also add operations to <strong>appendleft</strong>, adding an element as the first of the queue, and <strong>pop</strong> to remove the last element. Note that, while <strong>appendleft</strong> is done in constant time O(1), the <strong>pop</strong> operation needs to traverse the full list until it finds the last element to remove, thus O(n):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a93596ad88416dd09f32dcbb2c60abe/href">https://medium.com/media/7a93596ad88416dd09f32dcbb2c60abe/href</a></iframe><p>Next, we can add methods to <strong>find</strong> and <strong>remove </strong>a node given a certain data passed as an argument. Note that in a Doubly-Linked List, those processes would be simpler, since once we found the node, we can update the predecessor and successor at once. In a Singly-Linked we need to keep a reference to the predecessor, so that when removing we update the successor accordingly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b87481aa7ed75021e3d210e2f83b0f4/href">https://medium.com/media/2b87481aa7ed75021e3d210e2f83b0f4/href</a></iframe><p><strong>Container Type Methods</strong></p><p>In Python, objects that contain references to other objects are called <em>containers</em>, i.e: lists, tuples, and dictionaries. As an approach to <em>operator overloading</em>, Python provides invokes certain methods by special syntax. For instance, accessing an object with x[i] is equivalent to type(x).__getitem__(x, i).</p><p>Thus, for convenience, we can implement some of them as the code below suggests:</p><ul><li><a href="https://docs.python.org/3/reference/datamodel.html#object.__len__"><strong>__len__</strong></a>: Called to implement the built-in function <a href="https://docs.python.org/3/library/functions.html#len">len()</a></li></ul><pre>stack = SinglyLinkedList()<br>print(len(stack)) # 0</pre><ul><li><a href="https://docs.python.org/3/reference/datamodel.html#object.__contains__"><strong>__contains__</strong></a><strong>: </strong>Called to implement membership test operators. Should return true if <em>item</em> is in <em>self</em>, false otherwise. Uses __iter__()</li></ul><pre>stack = SinglyLinkedList(SLLnode(&#39;John Doe&#39;))<br>print(&#39;John Doe&#39; in stack) # True</pre><ul><li><a href="https://docs.python.org/3/reference/datamodel.html#object.__getitem__"><strong>__getitem__</strong></a>: Called to implement evaluation of self[key]</li></ul><pre>stack = SinglyLinkedList(SLLnode(&#39;John Doe&#39;))<br>print(stack[0]) # &#39;John Doe&#39;</pre><ul><li><a href="https://docs.python.org/3/reference/datamodel.html#object.__setitem__"><strong>__setitem__</strong></a>: Called to implement assignment to self[key]</li></ul><pre>stack = SinglyLinkedList()<br>stack[0] = SLLNode(&#39;Ada Lovelace&#39;)<br>print(stack[0]) # &#39;Ada Lovelace&#39;</pre><ul><li><a href="https://docs.python.org/3/reference/datamodel.html#object.__delitem__"><strong>__delitem__</strong></a><strong>:</strong> Called to implement deletion of self[key]</li></ul><pre>stack = SinglyLinkedList(SLLNode(&#39;Ada Lovelace&#39;))<br>print(len(stack)) # 1<br>del stack[0]<br>print(len(stack)) # 0</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e57ef2cee0d06c6a45b082f8f34b632/href">https://medium.com/media/4e57ef2cee0d06c6a45b082f8f34b632/href</a></iframe><p>The complete code can be found at this gist: <a href="https://gist.github.com/hspedro/253afbda49f383f4e7b17e9ae73ab63a">https://gist.github.com/hspedro/253afbda49f383f4e7b17e9ae73ab63a</a></p><h4>Other Resources</h4><p>This code helped me through multiple code interviews, and also to revisit some basic data structures fundamentals. I <strong>strongly</strong> recommend checking on these other resources!</p><ul><li><a href="http://www.laurentluce.com/posts/python-list-implementation/">List Implementation in CPython</a>, Laurent Luce</li><li><a href="https://docs.python.org/3/tutorial/datastructures.html#more-on-lists">Official Doc for Lists</a>, Python Docs</li><li><a href="https://docs.python.org/3/faq/design.html#how-are-lists-implemented-in-cpython">Design Details on List Implementation</a>, Python Docs</li><li><a href="https://realpython.com/linked-lists-python/#understanding-linked-lists">Understanding Linked Lists</a>, Real Python</li><li><a href="https://www.algorist.com/">The Algorithm Design Manual - Chapter 3</a>, Steven Skiena</li><li><a href="https://medium.com/basecs/whats-a-linked-list-anyway-part-1-d8b7e6508b9d">What’s A Linked List Anyway</a>, Vaidehi Joshi</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d881af444858" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>