{"id":9883,"date":"2021-07-09T11:02:26","date_gmt":"2021-07-09T05:32:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=9883"},"modified":"2021-11-22T18:50:11","modified_gmt":"2021-11-22T13:20:11","slug":"find-remove-node-linked-lists","status":"publish","type":"post","link":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/","title":{"rendered":"Singly Linked List: How To Find and Remove a Node"},"content":{"rendered":"

Prerequisites<\/h3>\n

To learn about singly linked lists, you should know:<\/p>\n

    \n
  1. Python 3<\/li>\n
  2. OOP concepts<\/li>\n
  3. Singly linked list – inserting a node and printing the nodes<\/li>\n<\/ol>\n

    What will we learn?<\/h3>\n

    In the last tutorial, we discussed what singly linked lists are, how to add a node and how to print all the nodes. We strongly recommend you to read that first if you haven\u2019t as we will be building off of those concepts.<\/p>\n

    This tutorial is about how to remove a node and how to know if a particular element exists in the linked list or not. Both these methods belong to the\u00a0LinkedList\u00a0<\/i>class. Let us see these one by one.<\/p>\n

    How to find an element?<\/h3>\n

    Like in most data structures, the only way to find if an element exists is by traversing the entire linked list. Note that if the linked list is sorted, we can use binary search. But here we are going to consider an unsorted linked list. The way this works is that the user will give us an element and we return\u00a0True\u00a0<\/i>if we find the element else we return\u00a0False<\/i>. You can also use a counter and return the index of the element if it exists.<\/p>\n

    Algorithm<\/h3>\n
      \n
    1. Set a pointer\u00a0curr\u00a0<\/i>to the\u00a0head<\/i><\/li>\n
    2. Compare\u00a0curr.data\u00a0<\/i>to the input value:\n
        \n
      1. If equal, return\u00a0True<\/i><\/li>\n
      2. Else, move to the next pointer<\/li>\n<\/ol>\n<\/li>\n
      3. Repeat steps 1-2 until the element is found or the end of the linked list is met<\/li>\n<\/ol>\n

        Code<\/p>\n

        def findNode(self,value):\r\n\r\n       curr = self.head\r\n       while curr:\r\n           if curr.getData() == value:\r\n               return True\r\n           curr = curr.getNextNode()\r\n       return False<\/pre>\n

        How to remove nodes from singly linked lists?<\/h3>\n

        Now that you know how to find a node, we can use this to remove a node. Again, there are several approaches to do this. You can remove the first node or you can remove the last node by maintaining a\u00a0tail\u00a0<\/i>pointer that points to the last node of the linked list. The approach we are discussing here is that we get a value from the user, find the element in the linked list and remove it if it exists. It is very important to let the user know if the element was successfully removed. For this purpose, we return\u00a0True\u00a0<\/i>for success and\u00a0False\u00a0<\/i>otherwise. Remember to decrement the\u00a0size\u00a0<\/i>attribute by 1.<\/p>\n

        Let us call the node to be deleted as the current node. The idea is to link the previous node\u2019s next to point to the current node\u2019s next node. For example, let us say we want to delete 4 from the given linked list:<\/p>\n

        Linked list: H-->3-->4-->5 \r\n\r\nRemove 4: H-->3-->5<\/pre>\n

        We made the 3\u2019s next node point to 4\u2019s next node which is 5.<\/p>\n

        Let us say we want to delete 3<\/p>\n

        Remove 3: H-->5<\/pre>\n

        We made the head pointer to point to 3\u2019s next which is 5.<\/p>\n

        Note:<\/strong>\u00a0To do make a connection between the previous node and the current node\u2019s next node, it is important to keep track of the previous node.<\/p>\n

        Algorithm<\/h3>\n
          \n
        1. Have two pointers:\n
            \n
          1. curr\u00a0–<\/i>\u00a0Initially\u00a0points\u00a0to\u00a0head<\/i><\/li>\n
          2. prev –\u00a0initially points to\u00a0None<\/em><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n
              \n
            1. <\/li>\n
            2. If inputted value matches the data of\u00a0curr<\/i>:\n
                \n
              1. Check\u00a0prev\u00a0<\/i>exists:\n
                  \n
                1. If yes, set next node of\u00a0prev\u00a0<\/i>to next node of\u00a0curr<\/i><\/li>\n
                2. If no, simply point the\u00a0head\u00a0<\/i>to next node of\u00a0curr\u00a0<\/i>(this happens when you want to delete the first node)<\/li>\n
                3. Decrement\u00a0size\u00a0<\/i>by 1<\/li>\n
                4. Return\u00a0True<\/i><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n
                5. If inputted value doesn\u2019t match the data of\u00a0curr<\/i>:\n
                    \n
                  1. Proceed to next node by:\n
                      \n
                    1. Pointing\u00a0prev\u00a0<\/i>to\u00a0curr<\/i><\/li>\n
                    2. Pointing\u00a0curr\u00a0<\/i>to next node of\u00a0curr<\/i><\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n
                    3. Repeat steps 1-3 till the end of the linked list<\/li>\n
                    4. If the end of the linked list is reached, return\u00a0False<\/i>\u00a0indicating no element in the linked list matches the inputted value<\/li>\n<\/ol>\n

                      Code<\/p>\n

                      def removeNode(self,value):\r\n\r\n        prev = None\r\n        curr = self.head\r\n        while curr:\r\n            if curr.getData() == value:\r\n                if prev:\r\n                    prev.setNextNode(curr.getNextNode())\r\n                else:\r\n                    self.head = curr.getNextNode()\r\n                return True\r\n                    \r\n            prev = curr\r\n            curr = curr.getNextNode()\r\n            \r\n        return False<\/pre>\n

                      Conclusion<\/h3>\n

                      That\u2019s it for this tutorial. In the future tutorials, we will see how to reverse a singly linked list. Happy Pythoning!<\/p>\n","protected":false},"excerpt":{"rendered":"

                      Prerequisites To learn about singly linked lists, you should know: Python 3 OOP concepts Singly linked list – inserting a node and printing the nodes What will we learn? In the last tutorial, we discussed what singly linked lists are, how to add a node and how to print all the nodes. We strongly recommend …<\/p>\n

                      Singly Linked List: How To Find and Remove a Node<\/span> Read More »<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nSingly Linked List: How To Find and Remove a Node - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Singly Linked List: How To Find and Remove a Node - Python Programs\" \/>\n<meta property=\"og:description\" content=\"Prerequisites To learn about singly linked lists, you should know: Python 3 OOP concepts Singly linked list – inserting a node and printing the nodes What will we learn? In the last tutorial, we discussed what singly linked lists are, how to add a node and how to print all the nodes. We strongly recommend … Singly Linked List: How To Find and Remove a Node Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-09T05:32:26+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:20:11+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"veer\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#webpage\",\"url\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/\",\"name\":\"Singly Linked List: How To Find and Remove a Node - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-07-09T05:32:26+00:00\",\"dateModified\":\"2021-11-22T13:20:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Singly Linked List: How To Find and Remove a Node\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/9f9e30fd3f415217a11ac0d939213b7f\"},\"headline\":\"Singly Linked List: How To Find and Remove a Node\",\"datePublished\":\"2021-07-09T05:32:26+00:00\",\"dateModified\":\"2021-11-22T13:20:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#webpage\"},\"wordCount\":629,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/9f9e30fd3f415217a11ac0d939213b7f\",\"name\":\"veer\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/f1f6915d5328abaea9a64249313d1c55?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/f1f6915d5328abaea9a64249313d1c55?s=96&d=mm&r=g\",\"caption\":\"veer\"},\"sameAs\":[\"https:\/\/python-programs.com\"],\"url\":\"https:\/\/python-programs.com\/author\/veer\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Singly Linked List: How To Find and Remove a Node - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/","og_locale":"en_US","og_type":"article","og_title":"Singly Linked List: How To Find and Remove a Node - Python Programs","og_description":"Prerequisites To learn about singly linked lists, you should know: Python 3 OOP concepts Singly linked list – inserting a node and printing the nodes What will we learn? In the last tutorial, we discussed what singly linked lists are, how to add a node and how to print all the nodes. We strongly recommend … Singly Linked List: How To Find and Remove a Node Read More »","og_url":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-07-09T05:32:26+00:00","article_modified_time":"2021-11-22T13:20:11+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"veer","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#webpage","url":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/","name":"Singly Linked List: How To Find and Remove a Node - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-07-09T05:32:26+00:00","dateModified":"2021-11-22T13:20:11+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/find-remove-node-linked-lists\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Singly Linked List: How To Find and Remove a Node"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/9f9e30fd3f415217a11ac0d939213b7f"},"headline":"Singly Linked List: How To Find and Remove a Node","datePublished":"2021-07-09T05:32:26+00:00","dateModified":"2021-11-22T13:20:11+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/find-remove-node-linked-lists\/#webpage"},"wordCount":629,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/find-remove-node-linked-lists\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/9f9e30fd3f415217a11ac0d939213b7f","name":"veer","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/f1f6915d5328abaea9a64249313d1c55?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f1f6915d5328abaea9a64249313d1c55?s=96&d=mm&r=g","caption":"veer"},"sameAs":["https:\/\/python-programs.com"],"url":"https:\/\/python-programs.com\/author\/veer\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/9883"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=9883"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/9883\/revisions"}],"predecessor-version":[{"id":9884,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/9883\/revisions\/9884"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=9883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=9883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=9883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}