-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathDelete every N nodes in LL.cpp
More file actions
84 lines (79 loc) · 1.38 KB
/
Delete every N nodes in LL.cpp
File metadata and controls
84 lines (79 loc) · 1.38 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
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
Node(int d){
data = d;
next = NULL;
}
};
Node *takeinput(){
int data;
cin>>data;
Node *head = NULL;
Node *tail = NULL;
while(data != -1){
Node *newnode = new Node(data);
if(head ==NULL){
head = newnode;
tail = newnode;
}
else{
tail->next = newnode;
tail=tail->next;
}
cin>>data;
}
return head;
}
void print(Node *head){
Node*temp = head;
while(temp!=NULL){
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<endl;
}
Node * deleteEveryNNodes(Node * head, int m , int n){
if(m==0)
return NULL;
else{
int c1 = 1, c2 = 1;
Node * t1 = head;
while(t1!= NULL){
while(c1!=m){
if(t1==NULL)
break;
t1 = t1->next;
c1++;
}
Node * t2 = t1->next;
while(c2!=n){
if(t2==NULL)
break;
t2 = t2->next;
c2++;
}
if(t2==NULL){
t1->next = NULL;
t1 = t1->next;
}
else{
t2 = t2->next;
t1->next = t2;
t1= t2;}
c1 = 1;
c2 = 1;
}
return head;
}
}
int main(){
Node *head = takeinput();
int m,n;
cin>>m>>n;
Node * h = deleteEveryNNodes(head,m,n);
print(h);
}