Delete smaller nodes in linked list Python program

I tried to write following function to delete the smallest node in a linked list. But, i am getting error and am not able to rectify it. please help.

error is -> Error 1 error C4703: potentially uninitialized local pointer variable 'trailSmall' used

here is my code:

void linkedListType::deleteSmallest() { nodeType *current; nodeType *trailCurrent; // used for pointing to node just before current nodeType *small; nodeType *trailSmall; if (first == NULL) cout << "Cannot delete from an empty list." << endl; else if (first->link == NULL) { first = NULL; delete last; last = NULL; } else { small = first; trailCurrent = first; current = first->link; while (current != NULL) { if (small->info > current->info) { trailSmall = trailCurrent; small = current; } trailCurrent = current; current = current->link; } if (small == first) first = first->link; else if (small != first) { trailSmall->link = small->link; if (small == last) last = trailSmall; } delete small; } }

Last update on January 04 2021 14:02:46 (UTC/GMT +8 hours)

Write a Python program to delete the last item from a singly linked list.

Sample Solution:-

Python Code:

class Node: # Singly linked node def __init__(self, data=None): self.data = data self.next = None class singly_linked_list: def __init__(self): # Createe an empty list self.tail = None self.head = None self.count = 0 def append_item(self, data): #Append items on the list node = Node(data) if self.head: self.head.next = node self.head = node else: self.tail = node self.head = node self.count += 1 def delete_item(self, data): # Delete an item from the list current = self.tail prev = self.tail while current: if current.data == data: if current == self.tail: self.tail = current.next else: prev.next = current.next self.count -= 1 return prev = current current = current.next def iterate_item(self): # Iterate the list. current_item = self.tail while current_item: val = current_item.data current_item = current_item.next yield val items = singly_linked_list() items.append_item('PHP') items.append_item('Python') items.append_item('C#') items.append_item('C++') items.append_item('Java') print("Original list:") for val in items.iterate_item(): print(val) print("\nAfter removing the last item from the list:") items.delete_item('Java') for val in items.iterate_item(): print(val)

Sample Output:

Original list: PHP Python C# C++ Java After removing the last item from the list: PHP Python C# C++

Flowchart:

Delete smaller nodes in linked list Python program

Python Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Python program to delete the first item from a singly linked list.
Next: Write a Python program to create a doubly linked list, append some items and iterate through the list (print forward).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Returns the items that are parity outliers:

Example:

from collections import Counter def find_parity_outliers(nums): return [ x for x in nums if x % 2 != Counter([n % 2 for n in nums]).most_common()[0][0] ] print(find_parity_outliers([1, 2, 3, 4, 5, 6]))

Output:

[2, 4, 6]

Delete nodes from the linked list which have a greater value on the right side

In this problem, we have given a singly linked list, and we need to remove all the nodes from it which have a greater value on the right side.

Examples:

  • Suppose the list 13 -> 16 -> 9 -> 10 -> 4 -> 6 -> 1 -> 3 -> NULL. This list will be changed to 16 -> 10 -> 6 -> 3 -> NULL. In this, the nodes 13, 9, 4, 1 have been deleted because in linked list there is a greater value on the right side.
  • The list 90 -> 80 -> 70 -> 60 -> 50 -> NULL will not be changed because there is no node which has the greater value on the right side.

Method:

We can do this by using the following steps:

  • Firstly, we will reverse the given linked list.
  • Then, we will traverse the reversed linked list and keep the max node. If the next node is less than the max node, we will delete the next node, otherwise max = next node.
  • Finally, we will reverse the linked list again to maintain the order of the linked list given earlier.

C program to delete the nodes which have a greater value on the right side

#include #include struct node { int info; struct node *next; }; struct node *start = NULL; // For inserting the elements in the linked list void add(int item) { struct node *t, *p; t = (struct node *)malloc( sizeof( struct node )); if(start == NULL) { start = t; start -> info = item; start -> next = NULL; return; } else { struct node *p = start; while(p -> next != NULL) { p = p -> next; } p -> next = t; p = p -> next; p -> info = item; p -> next = NULL; } } // For reversing the nodes of the linked list void reverse (struct node * t) {     struct node * curr = t;     struct node * next = NULL;     struct node * pre = NULL;     while(curr != NULL)     {         next=curr -> next;         curr -> next = pre;         pre = curr;         curr = next;    }    start = pre; } void deleteLesser(struct node * temp) {             reverse(temp); // For reversing the linked list             delete(start); // For delete the nodes which have a node with greater value on left side.             reverse(start); // For reversing the linked list again to maintain the original order of the linked list } // Function for delete the nodes which have a node with greater value on the left side. void delete(struct node * start) {             struct node * curr = start;             struct node * max = start;             struct node * temp;             while( curr != NULL && curr -> next != NULL)             {                         if(curr -> next -> info < max -> info) // If the curr is smaller than the max, then delete the curr                         {                                     temp = curr -> next;                                     curr -> next = temp -> next;                         }                         else                         {                                     curr = curr -> next;                                     max = curr;                         }             } } // To display the elements of the linked list void traverse(struct node * t) { if(t == NULL) {             printf(" Linked list is empty\n");                                     }                                     while(t -> next != NULL)                                     {                         printf("%d -> ",t -> info);                         t = t -> next;                         }                         printf("%d\n",t -> info); } // Driver Function int main() {     add(13);     add(16);     add(9);     add(10);     add(4);     add(6);     add(1);     add(3);     printf("Given Linked List: \n");     traverse(start);     deleteLesser(start);     printf("Linked List After deletion: \n");     traverse(start);     return 0; }

Output:

Delete smaller nodes in linked list Python program

Time Complexity: The time complexity of the above method is O(n).