-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlec3_7.cpp
More file actions
117 lines (98 loc) Β· 2.08 KB
/
lec3_7.cpp
File metadata and controls
117 lines (98 loc) Β· 2.08 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
https://www.geeksforgeeks.org/problems/remove-every-kth-node/1?utm_source=geeksforgeeks&utm_medium=article_practice_tab&utm_campaign=article_practice_tab
//{ Driver Code Starts
// C program to find n'th Node in linked list
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
/* Link list Node */
struct Node
{
int data;
struct Node* next;
Node(int x){
data = x;
next = NULL;
}
};
void append(struct Node** head_ref, struct Node **tail_ref, int new_data)
{
struct Node* new_node = new Node(new_data);
if (*head_ref == NULL)
*head_ref = new_node;
else
(*tail_ref)->next = new_node;
*tail_ref = new_node;
}
// } Driver Code Ends
/* Link list Node
struct Node
{
int data;
struct Node* next;
Node(int x){
data = x;
next = NULL;
}
};
*/
/*You are required to complete this method*/
/* Function to get the middle of the linked list*/
/*K will always be in range */
class Solution {
public:
Node* deleteK(Node *head,int K){
//Your code here
if(K==1)
{
return NULL;
}
Node *curr = head , *prev = NULL;
int count = 1;
while(curr)
{
if(K == count)
{
prev->next = curr->next;
delete curr;
curr = curr->next;
count = 1;
}
else{
prev = curr;
curr = curr->next;
count++;
}
}
return head;
}
};
//{ Driver Code Starts.
/* Driver program to test above function*/
int main()
{
int T,i,n,l;
cin>>T;
while(T--){
struct Node *head = NULL, *tail = NULL;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>l;
append(&head, &tail, l);
}
int K;
cin>>K;
Solution obj;
Node *res = obj.deleteK(head,K);
Node *temp = res;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
return 0;
}
// } Driver Code Ends