Skip to content

Commit 304143a

Browse files
Added Arrays and Strings in array folder and linked list questions in linked list folder (#255)
* Added arrays and strings * added 500+ linked list questions in linked list folder
1 parent 16ff46c commit 304143a

File tree

164 files changed

+4184
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+4184
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: detectloop","url":"d:\\DSA-COMPETITIVE-CODING-master\\WEEK-4\\Lecture-47\\detectloop.cpp","tests":[],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\DSA-COMPETITIVE-CODING-master\\WEEK-4\\Lecture-47\\detectloop.cpp","group":"local","local":true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: test4","url":"d:\\DSA-COMPETITIVE-CODING-master\\WEEK-4\\Lecture-47\\test4.cpp","tests":[{"id":1662390825919,"input":"5","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\DSA-COMPETITIVE-CODING-master\\WEEK-4\\Lecture-47\\test4.cpp","group":"local","local":true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: test5","url":"d:\\DSA-COMPETITIVE-CODING-master\\WEEK-4\\Lecture-47\\test5.cpp","tests":[{"id":1662461248249,"input":"this is level 71","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"d:\\DSA-COMPETITIVE-CODING-master\\WEEK-4\\Lecture-47\\test5.cpp","group":"local","local":true}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp"
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Love Babbar Reverse K linked List .
2+
// leet code-25
3+
// Microsoft Interview
4+
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
#define ll long long
10+
#define fi(a,n) for(int i=a;i<n;i++)
11+
class node{
12+
public:
13+
int data;
14+
node*next;
15+
node(int data){
16+
this->data=data;
17+
this->next=NULL;
18+
}
19+
};
20+
21+
22+
void print(node*&head){
23+
node*temp=head;
24+
while (temp!=NULL)
25+
{
26+
cout<<temp->data<<" ";
27+
temp=temp->next;
28+
29+
}
30+
cout<<endl;
31+
}
32+
void insertathead(node*&head,int data){
33+
node*nodetoinsert=new node(data);
34+
nodetoinsert->next=head;
35+
head=nodetoinsert;
36+
}
37+
void insertattail(node*&tail,int data){
38+
node*nodetoinsert=new node(data);
39+
tail->next=nodetoinsert;
40+
tail=nodetoinsert;
41+
}
42+
void insertatposition(node*head,node*&tail,int data,int position){
43+
// if the list is empty
44+
if(head==NULL){
45+
insertathead(head,data);
46+
return;
47+
}
48+
int ct=1;
49+
node*temp=head;
50+
while (ct<=position-1)
51+
{
52+
temp=temp->next;
53+
ct++;
54+
}
55+
if(temp->next=NULL){
56+
insertattail(tail,data);
57+
return;
58+
}
59+
node*nodetoinsert=new node(data);
60+
nodetoinsert->next=temp->next;
61+
temp->next=nodetoinsert;
62+
63+
64+
}
65+
int detectcycle(node*&head){
66+
map<node*,bool>m;
67+
node*temp=head;
68+
while (temp!=NULL)
69+
{
70+
if(m[temp]==true)
71+
return 1;
72+
// if it is circular
73+
m[temp]=true;
74+
temp=temp->next;
75+
76+
}
77+
return 0;
78+
}
79+
int main(){
80+
ios_base::sync_with_stdio(false);
81+
cin.tie(NULL);
82+
83+
node *n1=new node(5);
84+
85+
node*head=n1;
86+
node*tail=n1;
87+
88+
insertathead(head,4);
89+
insertathead(head,3);
90+
insertathead(head,2);
91+
insertathead(head,1);
92+
insertattail(tail,6);
93+
print(head);
94+
tail->next=head->next->next;
95+
cout<< detectcycle(head)<<endl;
96+
97+
98+
return 0;
99+
}
100+
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// Love Babbar Reverse K linked List .
2+
// leet code-25
3+
// Microsoft Interview
4+
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
#define ll long long
10+
#define fi(a,n) for(int i=a;i<n;i++)
11+
class node{
12+
public:
13+
int data;
14+
node*next;
15+
node(int data){
16+
this->data=data;
17+
this->next=NULL;
18+
}
19+
20+
};
21+
22+
23+
void print(node*&head){
24+
node*temp=head;
25+
while (temp!=NULL)
26+
{
27+
cout<<temp->data<<" ";
28+
temp=temp->next;
29+
30+
}
31+
cout<<endl;
32+
}
33+
void insertathead(node*&head,int data){
34+
node*nodetoinsert=new node(data);
35+
nodetoinsert->next=head;
36+
head=nodetoinsert;
37+
}
38+
void insertattail(node*&tail,int data){
39+
node*nodetoinsert=new node(data);
40+
tail->next=nodetoinsert;
41+
tail=nodetoinsert;
42+
}
43+
void insertatposition(node*head,node*&tail,int data,int position){
44+
// if the list is empty
45+
if(head==NULL){
46+
insertathead(head,data);
47+
return;
48+
}
49+
int ct=1;
50+
node*temp=head;
51+
while (ct<=position-1)
52+
{
53+
temp=temp->next;
54+
ct++;
55+
}
56+
if(temp->next=NULL){
57+
insertattail(tail,data);
58+
return;
59+
}
60+
node*nodetoinsert=new node(data);
61+
nodetoinsert->next=temp->next;
62+
temp->next=nodetoinsert;
63+
64+
65+
}
66+
node* floyd_cycle(node*&head){
67+
if(head==NULL)
68+
return NULL;
69+
node*slow=head;
70+
node*fast=head;
71+
node*temp=head;
72+
while (slow!=NULL&& fast!=NULL)
73+
{
74+
fast=fast->next;
75+
if(fast!=NULL){
76+
fast=fast->next;
77+
}
78+
slow=slow->next;
79+
if(slow==fast)
80+
{
81+
return slow;
82+
}
83+
}
84+
return NULL;
85+
86+
}
87+
node*get_starting_node(node*head){
88+
if(head==NULL){
89+
return NULL;
90+
}
91+
node*low=head;
92+
node*get_intersection=floyd_cycle(head);
93+
94+
while (low!=get_intersection)
95+
{
96+
low=low->next;
97+
get_intersection=get_intersection->next;
98+
99+
}
100+
101+
return low;
102+
}
103+
104+
node*remove_circle(node*head){
105+
if(head==NULL)return NULL;
106+
node*low=get_starting_node(head);
107+
node*fast=get_starting_node(head);
108+
while (fast->next!=low)
109+
{
110+
fast=fast->next;
111+
}
112+
fast->next=NULL;
113+
return head;
114+
115+
}
116+
117+
int detectcycle(node*&head){
118+
if(head==NULL){
119+
return false;
120+
}
121+
map<node*,bool>m;
122+
node*temp=head;
123+
while (temp!=NULL)
124+
{
125+
if(m[temp]==true)
126+
return 1;
127+
// if it is circular
128+
m[temp]=true;
129+
temp=temp->next;
130+
131+
}
132+
return 0;
133+
}
134+
int main(){
135+
ios_base::sync_with_stdio(false);
136+
cin.tie(NULL);
137+
138+
node *n1=new node(5);
139+
140+
node*head=n1;
141+
node*tail=n1;
142+
143+
insertathead(head,4);
144+
insertathead(head,3);
145+
insertathead(head,2);
146+
insertathead(head,1);
147+
insertattail(tail,6);
148+
print(head);
149+
tail->next=head->next->next;
150+
cout<< detectcycle(head)<<endl;
151+
cout<<endl;
152+
153+
// if(floyd_cycle(head)){
154+
// cout<<"cycle is present "<<endl;
155+
// }
156+
// else
157+
// cout<<"Cycle is not present"<<endl;
158+
node*ans=get_starting_node(head);
159+
cout<<ans->data<<" ";
160+
node*ans2=remove_circle(head);
161+
cout<<endl;
162+
print(ans2);
163+
return 0;
164+
}
165+

0 commit comments

Comments
 (0)