Remove Linked List Elements
https://leetcode.com/problems/remove-linked-list-elements/
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head==NULL ) return head;
while(head!=NULL && head->val==val){
head=head->next;
}
ListNode* prev=NULL;
ListNode* temp=head;
while(temp!=NULL && temp->next!=nullptr){
if(temp->next->val==val)
temp->next = temp->next->next;
else temp=temp->next;
}
return head;
}
};
class Solution {
public:
ListNode* removeElements(ListNode* head, int val) {
if(head==NULL ) return head;
while(head!=NULL && head->val==val){
head=head->next;
}
ListNode* prev=NULL;
ListNode* temp=head;
while(temp!=NULL ){
if(temp->val==val){
prev->next=temp->next;
temp=temp->next;
}
else{
prev=temp;
temp=temp->next;
}
}
return head;
}
};
Comments
Post a Comment