2.2.3. ALGORITHMS & DATA STRUCTURES. LINKED LISTS. THE DELETE METHOD.

In this method, we need to do two things. First, we need to find a cell with the specified value, and second, we need to keep track of the previous cell. As soon as we find the required entry, we just need to create a new link from the previous node to the node with the found values. After this, we can delete the object from memory. Let's examine the implementation in the Python programming language.

    def delete(self, value):
        '''
        Delete an element based on its value.
        Complexity: O(n).
        '''
        current = self.top.next_node
        prev = self.top
        while current is not None:
            if current.value == value:
                prev.next_node = current.next_node
                return
            prev = current
            current = current.next_node        

In the first lines, we set the current and previous elements. The previous element is initially set as the limiter. Next, we simply iterate through all the elements. If the values of an element match the one we are searching for, we just update the references. Furthermore, please note that in the delete methods, we return without the current cell. This is because of Python's garbage collection mechanism. After references to an object cease to exist, it is automatically deleted from memory. We stop referencing it in this line: prev.next_node = current.next_node, when we change the next_node in the previous element. Garbage collection works not only in Python but also in Java, C#, and other programming languages. However, in some languages like C++, you may need to explicitly delete a cell from memory, for example, by using a function like free(current). That concludes our discussion of singly linked lists. Next will be doubly linked lists.

要查看或添加评论,请登录

Igor Balitskyi的更多文章

  • ??? PDF to DOCX Conversion using Python

    ??? PDF to DOCX Conversion using Python

    ?? Libraries and Tools: pdfplumber, python-docx. pdfplumber: Used for opening and processing PDF files.

  • PROTECTING AN SSH SERVER, PORT KNOCKING

    PROTECTING AN SSH SERVER, PORT KNOCKING

    In this article, I will discuss methods for securing an SSH server, which we use to connect to and control remote…

  • HTTP, HTTP2, HTTPS

    HTTP, HTTP2, HTTPS

    After a user enters a website address in the browser, a request is first formed, which is then sent through a network…

  • SORTING LINKED LISTS. PART 1.

    SORTING LINKED LISTS. PART 1.

    Often, there is a need to get values from linked lists in ascending or descending order, and for this purpose, there…

    1 条评论
  • Первый язык программирования

    Первый язык программирования

    Можно ли считать Паскаль коммерчески успешным языком с точки зрения начинающего программиста? Сравним Паскаль и Питон…

    1 条评论
  • ?? How to solve problems that seem unsolvable

    ?? How to solve problems that seem unsolvable

    Sometimes, programmers face complex algorithmic problems with no obvious solutions. In these situations, they have to…

    1 条评论
  • 2.3. ALGORITHMS & DATA STRUCTURES. DOUBLY LINKED LISTS.

    2.3. ALGORITHMS & DATA STRUCTURES. DOUBLY LINKED LISTS.

    Doubly linked lists differ from singly linked lists in that each node refers to both the next and previous nodes…

  • 2.2.2. ALGORITHMS & DATASTRUCTURES. LINKED LISTS. ANOTHER VARIANT OF OPTIMIZING THE APPEND METHOD. TIME COMPLEXITY O(1).

    2.2.2. ALGORITHMS & DATASTRUCTURES. LINKED LISTS. ANOTHER VARIANT OF OPTIMIZING THE APPEND METHOD. TIME COMPLEXITY O(1).

    The algorithm in the last article every time we insert a new element, the algorithm iterates through all the cells of…

  • 2.2. ALGORITHMS & DATASTRUCTURES. LINKED LISTS - OPTIMIZATION.

    2.2. ALGORITHMS & DATASTRUCTURES. LINKED LISTS - OPTIMIZATION.

    В последней статье мы рассмотрели связный список, у которого был метод append для добавления новых элементов в конец…

  • РАЗДЕЛ 2. SECTION 2. СТРУКТУРЫ ДАННЫХ. DATA STRUCTURES. СВЯЗНЫЕ СПИСКИ, LINKED LISTS

    РАЗДЕЛ 2. SECTION 2. СТРУКТУРЫ ДАННЫХ. DATA STRUCTURES. СВЯЗНЫЕ СПИСКИ, LINKED LISTS

    Структура данных — это контейнер для организации, хранения и обработки информации. A data structure is a container for…

社区洞察

其他会员也浏览了