Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SinglyLinkedListNode* removeNodes(SinglyLinkedListNode* listHead, int x) {
- SinglyLinkedListNode* tmp = listHead;
- int init_case = 0;
- // initial first item
- if (tmp->data <= x){
- tmp = tmp->next;
- }
- else {
- SinglyLinkedListNode* removethis = tmp;
- tmp->next = tmp->next->next;
- delete removethis;
- init_case = 1;
- }
- while (tmp->next != NULL){
- if ( tmp->next->data <= x){
- // tmp->next->data <= x; continue traversing the list
- tmp = tmp->next;
- }
- else {
- // tmp->next->data < x; remove node
- SinglyLinkedListNode* removethis = tmp->next;
- tmp->next = tmp->next->next;
- delete removethis;
- }
- }
- if (init_case){
- return listHead->next;
- }
- else{
- return listHead;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement