{"id":1800,"date":"2020-06-11T17:37:31","date_gmt":"2020-06-11T17:37:31","guid":{"rendered":"https:\/\/blog.prepbytes.com\/?p=1800"},"modified":"2022-11-21T09:29:39","modified_gmt":"2022-11-21T09:29:39","slug":"delete-nodes-from-linked-list","status":"publish","type":"post","link":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/","title":{"rendered":"DELETE NODES FROM LINKED LIST"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png\" alt=\"\" \/><\/p>\n<p>In the Below article, we are trying to delete smaller nodes in linked list and delete larger nodes in linked list. We have seen so many concepts in data structures and now at this stage we got so familiar with the major concepts such as deletion in linked list, reversing a linked list etc. <\/p>\n<h2>How to Delete Smaller Nodes in Linked List and Delete Larger Nodes in Linked List.<\/h2>\n<p>Given a linked list containing N nodes, if the (i+1)th node is greater than the ith node than delete the ith node (0&lt;=i&lt;=N\u22121), this repeats till there is no smaller element in the left side of any element.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre><code>The list 11-&gt;16-&gt;10-&gt;8-&gt;5-&gt;6-&gt;2-&gt;3-&gt;NULL should be changed to 11-&gt;10-&gt;8-&gt;5-&gt;2-&gt;NULL. \nNote that 16, 6, and 3 have been deleted because there is a smaller value on the left side.<\/code><\/pre>\n<p><strong>You are encouraged to try the problem on your own before looking at the solution.<\/strong><\/p>\n<p><a href=\"https:\/\/mycode.prepbytes.com\/problems\/linked-list\/DELNOGRERIT\" title=\"\"><\/a><\/p>\n<h2>Explanation of how to delete smaller nodes in linked list and delete larger nodes in linked list.<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1646634706787-delete-nodes-from-linked-list-01%20%281%29.png\" alt=\"\" \/><\/p>\n<h2>Solutions:<\/h2>\n\t\t\t\t\t\t<style>\r\n\t\t\t\t\r\n\t\t\t\t\t#tab_container_1801 {\r\n\toverflow:hidden;\r\n\tdisplay:block;\r\n\twidth:100%;\r\n\tborder:0px solid #ddd;\r\n\tmargin-bottom:30px;\r\n\t}\r\n\r\n#tab_container_1801 .tab-content{\r\n\tpadding:20px;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n\tmargin-top: 0px;\r\n\tbackground-color:#ffffff !important;\r\n\tcolor: #000000 !important;\r\n\tfont-size:16px !important;\r\n\tfont-family: Open Sans !important;\r\n\t\r\n\t\tborder: 1px solid #e6e6e6 !important;\r\n\t}\r\n#tab_container_1801 .wpsm_nav-tabs {\r\n    border-bottom: 0px solid #ddd;\r\n}\r\n#tab_container_1801 .wpsm_nav-tabs > li.active > a, #tab_container_1801 .wpsm_nav-tabs > li.active > a:hover, #tab_container_1801 .wpsm_nav-tabs > li.active > a:focus {\r\n\tcolor: #000000 !important;\r\n\tcursor: default;\r\n\tbackground-color: #ffffff !important;\r\n\tborder: 1px solid #e6e6e6 !important;\r\n}\r\n\r\n#tab_container_1801 .wpsm_nav-tabs > li > a {\r\n    margin-right: 0px !important; \r\n    line-height: 1.42857143 !important;\r\n    border: 1px solid #d5d5d5 !important;\r\n    border-radius: 0px 0px 0 0 !important; \r\n\tbackground-color: #e8e8e8 !important;\r\n\tcolor: #000000 !important;\r\n\tpadding: 15px 18px 15px 18px !important;\r\n\ttext-decoration: none !important;\r\n\tfont-size: 14px !important;\r\n\ttext-align:center !important;\r\n\tfont-family: Open Sans !important;\r\n}\r\n#tab_container_1801 .wpsm_nav-tabs > li > a:focus {\r\noutline: 0px !important;\r\n}\r\n\r\n#tab_container_1801 .wpsm_nav-tabs > li > a:before {\r\n\tdisplay:none !important;\r\n}\r\n#tab_container_1801 .wpsm_nav-tabs > li > a:after {\r\n\tdisplay:none !important ;\r\n}\r\n#tab_container_1801 .wpsm_nav-tabs > li{\r\npadding:0px !important ;\r\nmargin:0px;\r\n}\r\n\r\n#tab_container_1801 .wpsm_nav-tabs > li > a:hover , #tab_container_1801 .wpsm_nav-tabs > li > a:focus {\r\n    color: #000000 !important;\r\n    background-color: #e8e8e8 !important;\r\n\tborder: 1px solid #d5d5d5 !important;\r\n\t\r\n}\r\n#tab_container_1801 .wpsm_nav-tabs > li > a .fa{\r\n\r\nmargin-right:5px !important;\r\n\r\nmargin-left:5px !important;\r\n\r\n\r\n}\r\n\r\n\t\t#tab_container_1801 .wpsm_nav-tabs a{\r\n\t\t\tbackground-image: none;\r\n\t\t\tbackground-position: 0 0;\r\n\t\t\tbackground-repeat: repeat-x;\r\n\t\t}\r\n\t\t\t\r\n\r\n\r\n#tab_container_1801 .wpsm_nav-tabs > li {\r\n    float: left;\r\n    margin-bottom: -1px !important;\r\n\tmargin-right:0px !important; \r\n}\r\n\r\n\r\n#tab_container_1801 .tab-content{\r\noverflow:hidden !important;\r\n}\r\n\r\n\r\n@media (min-width: 769px) {\r\n\r\n\t#tab_container_1801 .wpsm_nav-tabs > li{\r\n\t\tfloat:left !important ;\r\n\t\t\t\tmargin-right:-1px !important;\r\n\t\t\t\t\t}\r\n\t#tab_container_1801 .wpsm_nav-tabs{\r\n\t\tfloat:none !important;\r\n\t\tmargin:0px !important;\r\n\t}\r\n\r\n\t#tab_container_1801 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1801 .wpsm_nav{\r\n\t\t\t}\r\n\r\n}\r\n\r\n\r\n\r\n@media (max-width: 768px) {\r\n\t#tab_container_1801 .wpsm_nav-tabs > li {\r\n\t\t\t\t\r\n\t}\r\n\t#tab_container_1801 .wpsm_nav{\r\n\t\t\t}\r\n}\r\n\r\n\r\n\t.wpsm_nav-tabs li:before{\r\n\t\tdisplay:none !important;\r\n\t}\r\n\r\n\t@media (max-width: 768px) {\r\n\t\t\t\t\r\n\t\t\t\t.wpsm_nav-tabs{\r\n\t\t\tmargin-left:0px !important;\r\n\t\t\tmargin-right:0px !important; \r\n\t\t\t\r\n\t\t}\r\n\t\t\t\t#tab_container_1801 .wpsm_nav-tabs > li{\r\n\t\t\tfloat:none !important;\r\n\t\t}\r\n\t\t\t\r\n\t}\t\t\t\t<\/style>\r\n\t\t\t\t<div id=\"tab_container_1801\" >\r\n\t \r\n\t\t\t\t\t<ul class=\"wpsm_nav wpsm_nav-tabs\" role=\"tablist\" id=\"myTab_1801\">\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  class=\"active\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1801_1\" aria-controls=\"tabs_desc_1801_1\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1801_2\" aria-controls=\"tabs_desc_1801_2\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>C++<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1801_3\" aria-controls=\"tabs_desc_1801_3\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Java<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t<li role=\"presentation\"  onclick=\"do_resize()\">\r\n\t\t\t\t\t\t\t\t<a href=\"#tabs_desc_1801_4\" aria-controls=\"tabs_desc_1801_4\" role=\"tab\" data-toggle=\"tab\">\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t<i class=\"fa fa-code\"><\/i> \t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t<span>Python<\/span>\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t\t\r\n\t\t\t\t\t\t\t\t<\/a>\r\n\t\t\t\t\t\t\t<\/li>\r\n\t\t\t\t\t\t\t\t\t\t\t <\/ul>\r\n\r\n\t\t\t\t\t  <!-- Tab panes -->\r\n\t\t\t\t\t  <div class=\"tab-content\" id=\"tab-content_1801\">\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane  in active \" id=\"tabs_desc_1801_1\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"c\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"c\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n #include &lt;stdio.h&gt;\r\n    #include&lt;stdlib.h&gt;\r\n     struct node{\r\n     int data;\r\n     struct node* next;\r\n    };\r\n      struct node* newnode(int x)\r\n     {\r\n     \/\/struct node* temp=new node();\r\n     struct node* temp;\r\n     temp= (struct node *)malloc(sizeof(struct node));\r\n     temp-&gt;data=x;\r\n     temp-&gt;next=NULL;\r\n     return temp;\r\n     }\r\n     struct node* reverse(struct node* head)\r\n     {\r\n       struct node* prev=NULL,*nxt=NULL,*current=head;\r\n       while(current!=NULL)\r\n       {\r\n         nxt = current-&gt;next; \r\n\r\n            \/\/ Reverse current node's pointer \r\n            current-&gt;next = prev; \r\n\r\n            \/\/ Move pointers one position ahead. \r\n            prev = current; \r\n            current = nxt;\r\n       }\r\n       return prev;\r\n     }\r\n    struct  node* find_less(struct node* head)\r\n     {\r\n      struct node* curr=head;\r\n      struct  node* mx=head,*temp;\r\n        while(curr!=NULL&amp;&amp;curr-&gt;next!=NULL)\r\n        {\r\n            if(curr-&gt;next-&gt;data&lt;mx-&gt;data)\r\n            {\r\n                temp=curr-&gt;next;\r\n                curr-&gt;next=temp-&gt;next;\r\n                free(temp);\r\n            }\r\n            else\r\n            {\r\n                curr=curr-&gt;next;\r\n                mx=curr;\r\n            }\r\n        }\r\n        return head;\r\n     }\r\n     struct node* sort(struct node* head)\r\n     {\r\n       head=reverse(head);\r\n       head=find_less(head);\r\n       head=reverse(head);\r\n       return head;\r\n     }\r\n     int main()\r\n    {\r\n    int t;scanf(&quot;%d&quot;,&amp;t);\r\n     while(t--)\r\n    {\r\n    int n;scanf(&quot;%d%d&quot;,&amp;n);\r\n    int x;scanf(&quot;%d&quot;,&amp;x);\r\n    struct node* head=newnode(x);\r\n    struct   node* headlist=head;\r\n    for(int i=1;i&lt;n;i++)\r\n    {\r\n     int y;scanf(&quot;%d&quot;,&amp;y);\r\n     head-&gt;next=newnode(y);\r\n     head=head-&gt;next;\r\n    }\r\n    headlist=sort(headlist);\r\n     while(headlist!=NULL)\r\n    {\r\n     printf(&quot;%d &quot;,headlist-&gt;data);\r\n      headlist=headlist-&gt;next;\r\n    }\r\n    printf(&quot;&#92;n&quot;);\r\n    }\r\n\r\n    return 0;\r\n    }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\r\n\r\n\r\n\r\n\r\n\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1801_2\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"cpp\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n#include &lt;bits\/stdc++.h&gt;\r\n     using namespace std;\r\n     struct node{\r\n     int data;\r\n     node* next;\r\n    };\r\n       node* newnode(int x)\r\n     {\r\n     node* temp=new node();\r\n     temp-&gt;data=x;\r\n     temp-&gt;next=NULL;\r\n     return temp;\r\n     }\r\n     node* reverse(node* head)\r\n     {\r\n       node* prev=NULL,*nxt=NULL,*current=head;\r\n       while(current!=NULL)\r\n       {\r\n         nxt = current-&gt;next; \r\n\r\n            \/\/ Reverse current node's pointer \r\n            current-&gt;next = prev; \r\n\r\n            \/\/ Move pointers one position ahead. \r\n            prev = current; \r\n            current = nxt;\r\n       }\r\n       return prev;\r\n     }\r\n     node* find_less(node* head)\r\n     {\r\n       node* curr=head;\r\n        node* mx=head,*temp;\r\n        while(curr!=NULL&amp;&amp;curr-&gt;next!=NULL)\r\n        {\r\n            if(curr-&gt;next-&gt;data&lt;mx-&gt;data)\r\n            {\r\n                temp=curr-&gt;next;\r\n                curr-&gt;next=temp-&gt;next;\r\n                free(temp);\r\n            }\r\n            else\r\n            {\r\n                curr=curr-&gt;next;\r\n                mx=curr;\r\n            }\r\n        }\r\n        return head;\r\n     }\r\n     node* sort(node* head)\r\n     {\r\n       head=reverse(head);\r\n       head=find_less(head);\r\n       head=reverse(head);\r\n       return head;\r\n     }\r\n\r\n     int main()\r\n    {\r\n    int t;cin&gt;&gt;t;\r\n     while(t--)\r\n    {\r\n    int n;\r\n    cin&gt;&gt;n; \r\n    int x;cin&gt;&gt;x;\r\n    node* head=newnode(x);\r\n    node* headlist=head;\r\n    for(int i=1;i&lt;n;i++)\r\n    {\r\n      int y;cin&gt;&gt;y;\r\n      head-&gt;next=newnode(y);\r\n      head=head-&gt;next;\r\n    }\r\n    headlist= sort(headlist);\r\n    while(headlist!=NULL)\r\n    {\r\n      cout&lt;&lt;headlist-&gt;data&lt;&lt;&quot; &quot;;\r\n      headlist=headlist-&gt;next;\r\n    }\r\n    cout&lt;&lt;&quot;&#92;n&quot;;\r\n    }\r\n\r\n    return 0;\r\n    }\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1801_3\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"java\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\n import java.util.*;\r\n    import java.io.*;\r\n    class LinkedList { \r\n    Node head; \r\n    class Node { \r\n        int data; \r\n        Node next; \r\n        Node(int d) \r\n        { \r\n            data = d; \r\n            next = null; \r\n        } \r\n    } \r\n    void delLesserNodes() \r\n    { \r\n        reverseList(); \r\n        _delLesserNodes(); \r\n\r\n        reverseList(); \r\n    } \r\n    void _delLesserNodes() \r\n    { \r\n        Node current = head; \r\n        Node maxnode = head; \r\n        Node temp; \r\n\r\n        while (current != null &amp;&amp; current.next != null) { \r\n            if (current.next.data &lt; maxnode.data) { \r\n                temp = current.next; \r\n                current.next = temp.next; \r\n                temp = null; \r\n            } \r\n            else { \r\n                current = current.next; \r\n                maxnode = current; \r\n            } \r\n        } \r\n    } \r\n    void push(int new_data) \r\n    { \r\n        Node new_node = new Node(new_data); \r\n        new_node.next = head; \r\n        head = new_node; \r\n    } \r\n    void reverseList() \r\n    { \r\n        Node current = head; \r\n        Node prev = null; \r\n        Node next; \r\n        while (current != null) { \r\n            next = current.next; \r\n            current.next = prev; \r\n            prev = current; \r\n            current = next; \r\n        } \r\n        head = prev; \r\n    } \r\n    void printList() \r\n    { \r\n        Node temp = head; \r\n        while (temp != null) { \r\n            System.out.print(temp.data + &quot; &quot;); \r\n            temp = temp.next; \r\n        } \r\n        System.out.println(); \r\n    } \r\n     public static void main(String[] args) \r\n    { \r\n        Scanner sc = new Scanner(System.in);\r\n        int t= sc.nextInt();\r\n        while(t-- &gt;0 ){\r\n            int n = sc.nextInt();\r\n        LinkedList llist = new LinkedList(); \r\n        int p[]=new int[n];\r\n            for(int i=0;i&lt;n;i++)\r\n            {\r\n                p[i] = sc.nextInt();\r\n            }\r\n        for(int i=n-1;i&gt;=0;i--)\r\n            {\r\n                llist.push(p[i]); \r\n            }\r\n\r\n        llist.delLesserNodes();  \r\n        llist.printList(); }\r\n    } \r\n    } \r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\t\t\t\t\t <div role=\"tabpanel\" class=\"tab-pane \" id=\"tabs_desc_1801_4\">\r\n\t\t\t\t\t\t\t\t<!-- wp:enlighter\/codeblock {\"language\":\"Python\"} -->\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"Python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\r\nclass Node:\r\n\tdef __init__(self, data):\r\n\t\tself.data = data\r\n\t\tself.next = None\r\n\r\nclass LinkedList:\r\n\r\n\tdef __init__(self):\r\n\t\tself.head = None\r\n\r\n\tdef push(self, new_data):\r\n\t\tnew_node = Node(new_data)\r\n\t\tnew_node.next = self.head\r\n\t\tself.head = new_node\r\n\r\n\tdef printList(self):\r\n\t\ttemp = self.head\r\n\t\twhile(temp):\r\n\t\t\tprint (temp.data,end=&quot; &quot;)\r\n\t\t\ttemp = temp.next\r\n\r\n\r\n\tdef reverse_it(self):\r\n\t\tprev = None\r\n\t\tcurr = self.head\r\n\r\n\t\twhile curr:\r\n\t\t\tnext = curr.next\r\n\t\t\tcurr.next = prev\r\n\t\t\tprev = curr\r\n\t\t\tcurr = next\r\n\r\n\t\tself.head = prev\r\n\r\n\t\treturn prev\r\n\r\n\tdef find_less(self):\r\n\r\n\t\thead = self.head\r\n\t\tcurr = head\r\n\t\tmx = head\r\n\r\n\t\twhile curr and curr.next:\r\n\t\t\t\r\n\t\t\tif curr.next.data &lt; mx.data:\r\n\t\t\t\ttemp = curr.next\r\n\t\t\t\tcurr.next = temp.next\r\n\t\t\t\tdel temp\r\n\r\n\t\t\telse:\r\n\t\t\t\tcurr = curr.next\r\n\t\t\t\tmx = curr\r\n\r\n\t\tself.head = head\r\n\r\n\tdef sort(self):\r\n\r\n\t\tself.reverse_it()\r\n\t\tself.find_less()\r\n\t\tself.reverse_it()\r\n\r\n\r\nllist = LinkedList()\r\nllist.push(11)\r\nllist.push(18)\r\nllist.push(20)\r\nllist.push(14)\r\nllist.push(15)\r\n\r\nllist.printList()\r\n\r\nllist.sort()\r\n\r\nprint()\r\nllist.printList()\r\n<\/pre>\r\n<!-- \/wp:enlighter\/codeblock -->\t\t\t\t\t\t <\/div>\r\n\t\t\t\t\t\t\t\r\n\t\t\t\t\t <\/div>\r\n\t\t\t\t\t \r\n\t\t\t\t <\/div>\r\n <script>\r\n\t\tjQuery(function () {\r\n\t\t\tjQuery('#myTab_1801 a:first').tab('show')\r\n\t\t});\r\n\t\t\r\n\t\t\t\tjQuery(function(){\r\n\t\t\tvar b=\"fadeIn\";\r\n\t\t\tvar c;\r\n\t\t\tvar a;\r\n\t\t\td(jQuery(\"#myTab_1801 a\"),jQuery(\"#tab-content_1801\"));function d(e,f,g){\r\n\t\t\t\te.click(function(i){\r\n\t\t\t\t\ti.preventDefault();\r\n\t\t\t\t\tjQuery(this).tab(\"show\");\r\n\t\t\t\t\tvar h=jQuery(this).data(\"easein\");\r\n\t\t\t\t\tif(c){c.removeClass(a);}\r\n\t\t\t\t\tif(h){f.find(\"div.active\").addClass(\"animated \"+h);a=h;}\r\n\t\t\t\t\telse{if(g){f.find(\"div.active\").addClass(\"animated \"+g);a=g;}else{f.find(\"div.active\").addClass(\"animated \"+b);a=b;}}c=f.find(\"div.active\");\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t});\r\n\t\t\r\n\r\n\t\tfunction do_resize(){\r\n\r\n\t\t\tvar width=jQuery( '.tab-content .tab-pane iframe' ).width();\r\n\t\t\tvar height=jQuery( '.tab-content .tab-pane iframe' ).height();\r\n\r\n\t\t\tvar toggleSize = true;\r\n\t\t\tjQuery('iframe').animate({\r\n\t\t\t    width: toggleSize ? width : 640,\r\n\t\t\t    height: toggleSize ? height : 360\r\n\t\t\t  }, 250);\r\n\r\n\t\t\t  toggleSize = !toggleSize;\r\n\t\t}\r\n\r\n\r\n\t<\/script>\r\n\t\t\t\t\r\n\t\t\t\n<p><strong>Conclusion<\/strong><\/p>\n<p>In the above article, we clearly understood how small nodes and large nodes are different, we have also tried  how to delete smaller nodes in linked list and delete larger nodes in linked list.To practice more problems on  Linked lists you can check out <a href=\"#\" title=\".\">.<\/a><\/p>\n<h2>FAQs<\/h2>\n<p><strong>1. What is the time complexity of deleting a node from the first in a linked list?<\/strong><br \/>\nThe time complexity of deleting a node from the first in a linked list is O(n).<\/p>\n<p><strong>2. What is the space complexity of deleting a linked list?<\/strong><br \/>\nTo delete a linked list we need a temporary and empty list to track the traversing. So, the space complexity is O(n).<\/p>\n<p><strong>3. What are the conditions for deleting a node in a linked list?<\/strong><\/p>\n<ul>\n<li>Find the previous node to the node which is to be deleted.<\/li>\n<li>Change the next of the previous node.<\/li>\n<li>Free memory for the memory which is to be deleted.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In the Below article, we are trying to delete smaller nodes in linked list and delete larger nodes in linked list. We have seen so many concepts in data structures and now at this stage we got so familiar with the major concepts such as deletion in linked list, reversing a linked list etc. How [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[125],"tags":[],"class_list":["post-1800","post","type-post","status-publish","format-standard","hentry","category-linked-list"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.8 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Delete Nodes from Linked List | Linked List | Prepbytes<\/title>\n<meta name=\"description\" content=\"Linked List Containing N Nodes, If the (i+1)th Node Is Greater Than the Ith Node Than Delete the Ith Node (0&lt;=i&lt;=n\u22121), This Repeats Till There Is No Smaller Element in the Left Side of Any Element.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Delete Nodes from Linked List | Linked List | Prepbytes\" \/>\n<meta property=\"og:description\" content=\"Linked List Containing N Nodes, If the (i+1)th Node Is Greater Than the Ith Node Than Delete the Ith Node (0&lt;=i&lt;=n\u22121), This Repeats Till There Is No Smaller Element in the Left Side of Any Element.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\" \/>\n<meta property=\"og:site_name\" content=\"PrepBytes Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prepbytes0211\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-11T17:37:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T09:29:39+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png\" \/>\n<meta name=\"author\" content=\"Prepbytes\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prepbytes\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\"},\"author\":{\"name\":\"Prepbytes\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\"},\"headline\":\"DELETE NODES FROM LINKED LIST\",\"datePublished\":\"2020-06-11T17:37:31+00:00\",\"dateModified\":\"2022-11-21T09:29:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\"},\"wordCount\":307,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png\",\"articleSection\":[\"Linked list articles\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\",\"url\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\",\"name\":\"Delete Nodes from Linked List | Linked List | Prepbytes\",\"isPartOf\":{\"@id\":\"http:\/\/43.205.93.38\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png\",\"datePublished\":\"2020-06-11T17:37:31+00:00\",\"dateModified\":\"2022-11-21T09:29:39+00:00\",\"description\":\"Linked List Containing N Nodes, If the (i+1)th Node Is Greater Than the Ith Node Than Delete the Ith Node (0&lt;=i&lt;=n\u22121), This Repeats Till There Is No Smaller Element in the Left Side of Any Element.\",\"breadcrumb\":{\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage\",\"url\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png\",\"contentUrl\":\"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/43.205.93.38\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linked list articles\",\"item\":\"https:\/\/prepbytes.com\/blog\/category\/linked-list\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"DELETE NODES FROM LINKED LIST\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/43.205.93.38\/#website\",\"url\":\"http:\/\/43.205.93.38\/\",\"name\":\"PrepBytes Blog\",\"description\":\"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING\",\"publisher\":{\"@id\":\"http:\/\/43.205.93.38\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/43.205.93.38\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/43.205.93.38\/#organization\",\"name\":\"Prepbytes\",\"url\":\"http:\/\/43.205.93.38\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"contentUrl\":\"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp\",\"width\":160,\"height\":160,\"caption\":\"Prepbytes\"},\"image\":{\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/prepbytes0211\/\",\"https:\/\/www.instagram.com\/prepbytes\/\",\"https:\/\/www.linkedin.com\/company\/prepbytes\/\",\"https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e\",\"name\":\"Prepbytes\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/43.205.93.38\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g\",\"caption\":\"Prepbytes\"},\"url\":\"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Delete Nodes from Linked List | Linked List | Prepbytes","description":"Linked List Containing N Nodes, If the (i+1)th Node Is Greater Than the Ith Node Than Delete the Ith Node (0&lt;=i&lt;=n\u22121), This Repeats Till There Is No Smaller Element in the Left Side of Any Element.","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:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/","og_locale":"en_US","og_type":"article","og_title":"Delete Nodes from Linked List | Linked List | Prepbytes","og_description":"Linked List Containing N Nodes, If the (i+1)th Node Is Greater Than the Ith Node Than Delete the Ith Node (0&lt;=i&lt;=n\u22121), This Repeats Till There Is No Smaller Element in the Left Side of Any Element.","og_url":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/","og_site_name":"PrepBytes Blog","article_publisher":"https:\/\/www.facebook.com\/prepbytes0211\/","article_published_time":"2020-06-11T17:37:31+00:00","article_modified_time":"2022-11-21T09:29:39+00:00","og_image":[{"url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png","type":"","width":"","height":""}],"author":"Prepbytes","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Prepbytes","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#article","isPartOf":{"@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/"},"author":{"name":"Prepbytes","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e"},"headline":"DELETE NODES FROM LINKED LIST","datePublished":"2020-06-11T17:37:31+00:00","dateModified":"2022-11-21T09:29:39+00:00","mainEntityOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/"},"wordCount":307,"commentCount":0,"publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png","articleSection":["Linked list articles"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/","url":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/","name":"Delete Nodes from Linked List | Linked List | Prepbytes","isPartOf":{"@id":"http:\/\/43.205.93.38\/#website"},"primaryImageOfPage":{"@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage"},"image":{"@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage"},"thumbnailUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png","datePublished":"2020-06-11T17:37:31+00:00","dateModified":"2022-11-21T09:29:39+00:00","description":"Linked List Containing N Nodes, If the (i+1)th Node Is Greater Than the Ith Node Than Delete the Ith Node (0&lt;=i&lt;=n\u22121), This Repeats Till There Is No Smaller Element in the Left Side of Any Element.","breadcrumb":{"@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#primaryimage","url":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png","contentUrl":"https:\/\/prepbytes-misc-images.s3.ap-south-1.amazonaws.com\/assets\/1645098042855-Article_302.png"},{"@type":"BreadcrumbList","@id":"https:\/\/prepbytes.com\/blog\/delete-nodes-from-linked-list\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/43.205.93.38\/"},{"@type":"ListItem","position":2,"name":"Linked list articles","item":"https:\/\/prepbytes.com\/blog\/category\/linked-list\/"},{"@type":"ListItem","position":3,"name":"DELETE NODES FROM LINKED LIST"}]},{"@type":"WebSite","@id":"http:\/\/43.205.93.38\/#website","url":"http:\/\/43.205.93.38\/","name":"PrepBytes Blog","description":"ONE-STOP RESOURCE FOR EVERYTHING RELATED TO CODING","publisher":{"@id":"http:\/\/43.205.93.38\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/43.205.93.38\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/43.205.93.38\/#organization","name":"Prepbytes","url":"http:\/\/43.205.93.38\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/","url":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","contentUrl":"https:\/\/blog.prepbytes.com\/wp-content\/uploads\/2025\/07\/uzxxllgloialmn9mhwfe.webp","width":160,"height":160,"caption":"Prepbytes"},"image":{"@id":"http:\/\/43.205.93.38\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/prepbytes0211\/","https:\/\/www.instagram.com\/prepbytes\/","https:\/\/www.linkedin.com\/company\/prepbytes\/","https:\/\/www.youtube.com\/channel\/UC0xGnHDrjUM1pDEK2Ka5imA"]},{"@type":"Person","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/3f7dc4ae851791d5947a7f99df363d5e","name":"Prepbytes","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/43.205.93.38\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/232042cd1a1ea0e982c96d2a2ec93fb70a8e864e00784491231e7bfe5a9e06b5?s=96&d=mm&r=g","caption":"Prepbytes"},"url":"https:\/\/prepbytes.com\/blog\/author\/gourav-jaincollegedekho-com\/"}]}},"_links":{"self":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/comments?post=1800"}],"version-history":[{"count":10,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions"}],"predecessor-version":[{"id":10661,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/posts\/1800\/revisions\/10661"}],"wp:attachment":[{"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/media?parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/categories?post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prepbytes.com\/blog\/wp-json\/wp\/v2\/tags?post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}