sir, i tried this question lnk:https://leetcode.com/problems/remove-linked-list-elements/
but it is giving undefined memory i think some problem related with function
/**
-
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:
void remove(ListNode *temp,ListNode *prev)
{
ListNode *del = temp;
prev -> next = temp -> next;
temp = temp -> next;
delete(del);
cout<val<<" "<val<<endl;
}ListNode* removeElements(ListNode* head, int data)
{
ListNode *temp = head;
ListNode *prev = NULL;
while(temp->next!=NULL)
{
if(temp->val == data)
{
remove(temp,prev);
cout<val<<" "<val<<endl;} else { prev = temp; temp = temp->next; } } return head;
}
};