forked from lakshyasaxena/CP-Algorithms-in-C_plus-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L__DELETE_MIDDLE_ELEMENT_OF_SINGLY_LINKED_LIST.cpp
144 lines (122 loc) · 3.38 KB
/
L__DELETE_MIDDLE_ELEMENT_OF_SINGLY_LINKED_LIST.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//
//
// Given a Singly Linked List
// You have to delete the middle node from it, expected time complexity O(n)
//
// The idea is to delete it in single traverse
// So we maintain two pointers
// slow pointer
// and
// fast pointer
// For better understanding, see the code
//
// Note: You can encounter with two of the cases
// case 1: When you have odd number of nodes
// In this case you simply have to delete the middle node
//
// case 2: When you have even number of nodes
// In this case you have two middle nodes, you have to delete the right one
//
//
// Consider following example for your reference
//
// head -> 2 -> 10 -> 20 -> 25 -> 100
// After Deleting Middle node (Odd case)
// head -> 2 -> 10 -> 25 -> 100
// After Deleting Middle node (Even case)
// head -> 2 -> 10 -> 100
//
//
class Singly_Linked_List_Node
{
public:
ll data;
Singly_Linked_List_Node *next=NULL;
};
typedef Singly_Linked_List_Node lis;
void fill_list(lis *&);
void insert_in_front_of_list(lis *&,ll);
void traverse_list(lis *);
//************************************************************************************************************
lis* delete_middle_node(lis *head)
{
if(head==NULL) // case 1: List is empty
{
cout<<"List is empty, No middle value, Nothing to delete \n\n";
return head;
}
if(head->next==NULL) // case 2: Last node to be deleted, here we also update head
{
cout<<"Middle Node value = "<<head->data<<" , Deleting it\n\n";
delete(head);
head=NULL;
return head;
}
lis* slow=head;
lis* fast=head;
lis *pre=NULL;
while(fast!=NULL && fast->next != NULL)
{
fast=fast->next->next;
pre=slow;
slow=slow->next;
}
cout<<"Middle Node value = "<<slow->data<<" , Deleting it\n\n";
pre->next=slow->next;
delete(slow);
return head;
}
//************************************************************************************************************
int main()
{
lis *head=NULL;
fill_list(head);
traverse_list(head);
head = delete_middle_node(head);
traverse_list(head);
head = delete_middle_node(head);
traverse_list(head);
head = delete_middle_node(head);
traverse_list(head);
head = delete_middle_node(head);
traverse_list(head);
head = delete_middle_node(head);
traverse_list(head);
head = delete_middle_node(head);
traverse_list(head);
return 0;
}
// this below function has nothing do to with delete middle function
void fill_list(lis *&head)
{
insert_in_front_of_list(head,10);
insert_in_front_of_list(head,20);
insert_in_front_of_list(head,25);
insert_in_front_of_list(head,40);
insert_in_front_of_list(head,50);
}
void insert_in_front_of_list(lis *&head,ll value)
{
lis *temp=new(lis);
temp->data=value;
if(head==NULL) // first node to be inserted
{
head=temp;
return;
}
temp->next=head;
head=temp;
}
void traverse_list(lis *head)
{
cout<<"List is as follows { ";
while(!head==NULL)
{
cout<<head->data<<" ";
head=head->next;
}
cout<<" } \n";
}