-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathlist-del-n-after-m.cpp
77 lines (67 loc) · 1.62 KB
/
list-del-n-after-m.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
75
76
77
// http://www.geeksforgeeks.org/delete-n-nodes-after-m-nodes-of-a-linked-list/
#include <iostream>
using namespace std;
class node {
public:
int data;
node *next;
node(int data) {
this->data = data;
this->next = NULL;
}
};
void deleteMN(node *head, int m, int n);
node* addnode(node *head, int data);
void printList(node *head);
void deleteMN(node *head, int m, int n) {
if (head == NULL || head->next == NULL)
return;
node *ptr1 = head, *ptr2 = head;
while (ptr2 != NULL) {
for (int i = 0; ptr2 != NULL && i < m; i++) {
ptr1 = ptr2;
ptr2 = ptr2->next;
}
if (ptr2 == NULL)
return;
for (int i = 0; ptr2 != NULL && i < n; i++) {
ptr2 = ptr2->next;
}
ptr1->next = ptr2;
}
}
node* addnode(node *head, int data) {
if (head == NULL)
return new node(data);
node *ptr = head;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = new node(data);
return head;
}
void printList(node *head) {
if (head == NULL)
return;
while (head != NULL) {
cout<<head->data<<" ";
head = head->next;
}
}
int main() {
node *head = NULL;
head = addnode(head, 1);
head = addnode(head, 2);
head = addnode(head, 3);
head = addnode(head, 4);
head = addnode(head, 5);
head = addnode(head, 6);
head = addnode(head, 7);
head = addnode(head, 8);
cout<<"list: ";
printList(head);
cout<<endl<<"retain m, delete n: "<<endl;
deleteMN(head, 3, 2);
printList(head);
cout<<endl<<endl;
return 0;
}