-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelLesserNodes.cpp
More file actions
94 lines (78 loc) · 1.44 KB
/
delLesserNodes.cpp
File metadata and controls
94 lines (78 loc) · 1.44 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//Deletes nodes in a ll which have a lesser value in the right side
#include <iostream>
using namespace std;
struct Node
{
int info;
Node* next;
};
Node* createNewNode(int n) {
Node* newNode = new Node;
newNode->next = NULL;
newNode->info = n;
return newNode;
}
void insertBeg(Node** head, int new_data) {
Node* newNode = createNewNode(new_data);
if(*head==NULL)
*head = newNode;
else {
newNode->next = *head;
*head = newNode;
}
}
void printList(Node* node) {
while(node != NULL) {
cout<<node->info<<" -> ";
node = node->next;
}
}
void reverseList(Node** head) {
Node* current = *head;
Node* prev = NULL;
Node* next;
while(current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*head = prev;
}
void delNode(Node* head) {
Node* current = head;
Node* maxNode = head;
Node* temp;
while(current != NULL && current->next != NULL) {
if(current->next->info < maxNode->info) {
temp = current->next;
current->next = temp->next;
delete temp;
}
else {
current = current->next;
maxNode = current;
}
}
}
void delLesserNodes(Node** head) {
reverseList(head);
delNode(*head);
reverseList(head);
}
int main() {
Node* a = NULL;
insertBeg(&a, 3);
insertBeg(&a, 2);
insertBeg(&a, 6);
insertBeg(&a, 5);
insertBeg(&a, 11);
insertBeg(&a, 10);
insertBeg(&a, 15);
insertBeg(&a, 12);
printList(a);
cout<<endl;
delLesserNodes(&a);
printList(a);
return 0;
}