Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. SinglyLinkedListNode* removeNodes(SinglyLinkedListNode* listHead, int x) {
  2.     SinglyLinkedListNode* tmp = listHead;
  3.     int init_case = 0;
  4.     // initial first item
  5.     if (tmp->data <= x){
  6.         tmp = tmp->next;
  7.     }
  8.     else {
  9.         SinglyLinkedListNode* removethis = tmp;
  10.         tmp->next = tmp->next->next;
  11.         delete removethis;
  12.         init_case = 1;
  13.     }
  14.    
  15.     while (tmp->next != NULL){
  16.        
  17.         if ( tmp->next->data <= x){
  18.             // tmp->next->data <= x; continue traversing the list
  19.             tmp = tmp->next;
  20.         }
  21.         else {
  22.             // tmp->next->data < x; remove node
  23.             SinglyLinkedListNode* removethis = tmp->next;
  24.             tmp->next = tmp->next->next;
  25.             delete removethis;
  26.         }
  27.     }
  28.    
  29.     if (init_case){
  30.         return listHead->next;
  31.     }
  32.      
  33.     else{
  34.         return listHead;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement