Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# DELETE NODES FROM LINKED LIST

Last Updated on November 21, 2022 by Prepbytes

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 to Delete Smaller Nodes in Linked List and Delete Larger Nodes in Linked List.

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<=i<=N−1), this repeats till there is no smaller element in the left side of any element.

Example:

``````The list 11->16->10->8->5->6->2->3->NULL should be changed to 11->10->8->5->2->NULL.
Note that 16, 6, and 3 have been deleted because there is a smaller value on the left side.``````

You are encouraged to try the problem on your own before looking at the solution.

See original problem statement here

## Solutions:

``` #include <stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node* next;
};
struct node* newnode(int x)
{
//struct node* temp=new node();
struct node* temp;
temp= (struct node *)malloc(sizeof(struct node));
temp->data=x;
temp->next=NULL;
return temp;
}
{
while(current!=NULL)
{
nxt = current->next;

// Reverse current node's pointer
current->next = prev;

// Move pointers one position ahead.
prev = current;
current = nxt;
}
return prev;
}
{
while(curr!=NULL&&curr->next!=NULL)
{
if(curr->next->data<mx->data)
{
temp=curr->next;
curr->next=temp->next;
free(temp);
}
else
{
curr=curr->next;
mx=curr;
}
}
}
{
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
int n;scanf("%d%d",&n);
int x;scanf("%d",&x);
for(int i=1;i<n;i++)
{
int y;scanf("%d",&y);
}
{
}
printf("\n");
}

return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;
struct node{
int data;
node* next;
};
node* newnode(int x)
{
node* temp=new node();
temp->data=x;
temp->next=NULL;
return temp;
}
{
while(current!=NULL)
{
nxt = current->next;

// Reverse current node's pointer
current->next = prev;

// Move pointers one position ahead.
prev = current;
current = nxt;
}
return prev;
}
{
while(curr!=NULL&&curr->next!=NULL)
{
if(curr->next->data<mx->data)
{
temp=curr->next;
curr->next=temp->next;
free(temp);
}
else
{
curr=curr->next;
mx=curr;
}
}
}
{
}

int main()
{
int t;cin>>t;
while(t--)
{
int n;
cin>>n;
int x;cin>>x;
for(int i=1;i<n;i++)
{
int y;cin>>y;
}
{
}
cout<<"\n";
}

return 0;
}
```
``` import java.util.*;
import java.io.*;
class Node {
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
void delLesserNodes()
{
reverseList();
_delLesserNodes();

reverseList();
}
void _delLesserNodes()
{
Node temp;

while (current != null && current.next != null) {
if (current.next.data < maxnode.data) {
temp = current.next;
current.next = temp.next;
temp = null;
}
else {
current = current.next;
maxnode = current;
}
}
}
void push(int new_data)
{
Node new_node = new Node(new_data);
}
void reverseList()
{
Node prev = null;
Node next;
while (current != null) {
next = current.next;
current.next = prev;
prev = current;
current = next;
}
}
void printList()
{
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t= sc.nextInt();
while(t-- >0 ){
int n = sc.nextInt();
int p[]=new int[n];
for(int i=0;i<n;i++)
{
p[i] = sc.nextInt();
}
for(int i=n-1;i>=0;i--)
{
llist.push(p[i]);
}

llist.delLesserNodes();
llist.printList(); }
}
}
```
```class Node:
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def push(self, new_data):
new_node = Node(new_data)

def printList(self):
while(temp):
print (temp.data,end=" ")
temp = temp.next

def reverse_it(self):
prev = None

while curr:
next = curr.next
curr.next = prev
prev = curr
curr = next

return prev

def find_less(self):

while curr and curr.next:

if curr.next.data < mx.data:
temp = curr.next
curr.next = temp.next
del temp

else:
curr = curr.next
mx = curr

def sort(self):

self.reverse_it()
self.find_less()
self.reverse_it()

llist.push(11)
llist.push(18)
llist.push(20)
llist.push(14)
llist.push(15)

llist.printList()

llist.sort()

print()
llist.printList()
```

Conclusion

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 MYCODE | Competitive Programming.

## FAQs

1. What is the time complexity of deleting a node from the first in a linked list?
The time complexity of deleting a node from the first in a linked list is O(n).

2. What is the space complexity of deleting a linked list?
To delete a linked list we need a temporary and empty list to track the traversing. So, the space complexity is O(n).

3. What are the conditions for deleting a node in a linked list?

• Find the previous node to the node which is to be deleted.
• Change the next of the previous node.
• Free memory for the memory which is to be deleted.