-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeletingANodeinLLWhoseLastNodepointsMiddle.cpp
More file actions
117 lines (95 loc) · 2.2 KB
/
Copy pathDeletingANodeinLLWhoseLastNodepointsMiddle.cpp
File metadata and controls
117 lines (95 loc) · 2.2 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
#include<iostream>
using namespace std;
typedef struct node{
int data;
struct node* next;
}node;
node* newNode(int data){
node* node=new(struct node);
node->data=data;
node->next=NULL;
return node;
}
node* insertAtEnd(node *head,int data){
if(head==NULL)
return(newNode(data));
else{
node* rider=head;
while(rider->next){
rider=rider->next;
}
rider->next=newNode(data);
return head;//i return head always only for this case its useful
}
}
void display(node *head){
node* rider=head;
cout<<endl;
while(rider!=NULL){
cout<<rider->data<<' ';
rider=rider->next;
}
}
node* deleteNode(node* head,int data){
node* slow=head;
node* fast=head;
while(fast->next!=NULL){
slow=slow->next;
fast=fast->next->next;
if(slow==fast){
cout<<"loop present.They meet at "<<fast->data<<" ";
break;
}
}
slow=head;
node* last=fast;
while(slow!=fast){
last=fast;
slow=slow->next;
fast=fast->next;
}
cout<<"\n loop starts at "<<slow->data<<" last node is "<<last->data;
last->next=NULL;
cout<<"list after deleting the loop ";
display(head);
if(head->data==data){
head=head->next;
return head;
}
node* rider=head->next;
node* prev=head;
while(rider!=NULL){
if(rider->data==data){
cout<<"\nprev node of node to be deleted"<<prev->data<<"" ;
prev->next=rider->next;
rider=prev->next;
cout<<"\nnext is "<<prev->next->data;
break;
}
prev=rider;
rider=rider->next;
}
cout<<"\n list after deleting the node";
display(head);
node* n1=head;
node* n2=head;
while(n2->next!=NULL){
n1=n1->next;
n2=n2->next->next;
}
cout<<"\nmiddle node after deleting is "<<n1->data;
cout<<"\nlast node after deleting is "<<n2->data;
n2->next=n1;
}
int main(){
node* head=NULL;
head=insertAtEnd(head,1);
head=insertAtEnd(head,2);
head=insertAtEnd(head,3);
head=insertAtEnd(head,4);
head=insertAtEnd(head,5);
head=insertAtEnd(head,6);
head->next->next->next->next->next->next=head->next->next;
// display(head);
deleteNode(head,3);
}