-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete_node_linked_list.cpp
More file actions
85 lines (73 loc) · 1.53 KB
/
delete_node_linked_list.cpp
File metadata and controls
85 lines (73 loc) · 1.53 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
/*Please note that it's Function problem i.e.
you need to write your solution in the form of Function(s) only.
Driver Code to call/invoke your function is mentioned above.*/
/* Link list Node
struct Node
{
int data;
Node* next;
};*/
/*You are required to complete below method*/
/* Driver program to test above function*/
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* next;
};
Node* deleteNode(Node *head,int x)
{
Node* temp = head;
Node* prev = NULL;
int pos=1;
if(x==1){
head = temp->next;
return head;
}
while(temp && pos!=x){
prev = temp;
temp=temp->next;
pos++;
}
prev->next = temp->next;
return head;
//Your code here
}
void printList(Node *head){
while (head != NULL)
{
cout << head->data << " ";
head = head->next;
}
cout << " ";
}
void append(struct Node** head_ref, struct Node **tail_ref, int new_data){
struct Node* new_node = new Node;
new_node->data = new_data;
new_node->next = NULL;
if (*head_ref == NULL)
*head_ref = new_node;
else
(*tail_ref)->next = new_node;
*tail_ref = new_node;
}
int main(){
int T,i,n,l;
// TO BE REMOVED
for (int i=0; i<2000; i++);
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 kk;
cin>>kk;
head = deleteNode(head,kk);
printList(head);
}
return 0;
}