-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path203_remove_ll_elements.cpp
74 lines (61 loc) · 1.58 KB
/
203_remove_ll_elements.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <bits/stdc++.h>
using namespace std;
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) {}
};
ListNode* removeElements(ListNode* head, int val) {
if(head == nullptr) {
return head;
}
//base case
while(head!=nullptr && head -> val == val) {
head = head -> next;
}
ListNode* prev = NULL;
ListNode* curr = head;
while(curr != nullptr) {
if(curr -> val == val) {
prev ->next = curr -> next;
curr = curr -> next;
}
else {
prev = curr;
curr = curr -> next;
}
}
return head;
}
// Function to print the linked list
void printList(ListNode* head) {
while (head != nullptr) {
cout << head->val << " ";
head = head->next;
}
cout << endl;
}
int main() {
// Creating a linked list for testing
ListNode* head = new ListNode(1);
ListNode* node2 = new ListNode(2);
ListNode* node3 = new ListNode(6);
ListNode* node4 = new ListNode(3);
ListNode* node5 = new ListNode(4);
ListNode* node6 = new ListNode(5);
ListNode* node7 = new ListNode(6);
head->next = node2;
node2->next = node3;
node3->next = node4;
node4->next = node5;
node5->next = node6;
node6->next = node7;
cout << "Original Linked List: ";
printList(head);
int valToRemove = 6;
ListNode* newHead = removeElements(head, valToRemove);
printList(newHead);
return 0;
}