Answers for "how to delete duplicate number in c++ through erase"

0

how to delete repeated element in stack c++

int* stack = (int*)malloc(10 * sizeof(int));
int size = 10;
int sp = -1;

bool isempty() {
    return (sp == -1);
}

bool isfull() {
    return (sp == size - 1);
}

void push(int x) {
    if (isfull()) {
        printf("Full!");
    }
    else {
        sp++;
        stack[sp] = x;
    }
}

int pop() {
    int x;
    if (isempty()) {
        printf("Empty!");
    }
    else {
        x = stack[sp];
        sp--;
    }
    return x;
}

void peek() {
    if (!isempty()) {
        printf("%d", stack[sp]);
    }
}

void clear() {
    while (!isempty()) {
        pop();
    }
}

void print() {
    if (!isempty()) {
        for (int i = 0; i < sp+1; i++) {
            printf("%d ", stack[i]);
        }
    }
    printf("\n");
}
Posted by: Guest on June-07-2021
-1

remove duplicates from sorted list solution in c++

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head == NULL || head->next == NULL) 
            return head;
        ListNode* temp=head;
        ListNode* cur=head->next;
        while(cur!=NULL)
        {
            if(cur->val==temp->val)
            {
               temp->next=cur->next;
            }else 
            {
                temp=cur;
            }
            cur=cur->next;
        }
        return head;
    }
};
Posted by: Guest on February-27-2022

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language