Skip to content

Commit 35ca7ef

Browse files
authored
Merge pull request #974 from ayush91985/main
added linkde list program
2 parents b4c62f9 + bd82e47 commit 35ca7ef

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// C++ program to detect loop in a linked list
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
/* Link list node */
6+
struct Node {
7+
int data;
8+
struct Node* next;
9+
};
10+
11+
void push(struct Node** head_ref, int new_data)
12+
{
13+
/* allocate node */
14+
struct Node* new_node = new Node;
15+
16+
/* put in the data */
17+
new_node->data = new_data;
18+
19+
/* link the old list off the new node */
20+
new_node->next = (*head_ref);
21+
22+
/* move the head to point to the new node */
23+
(*head_ref) = new_node;
24+
}
25+
26+
// Returns true if there is a loop in linked list
27+
// else returns false.
28+
bool detectLoop(struct Node* h)
29+
{
30+
unordered_set<Node*> s;
31+
while (h != NULL) {
32+
// If this node is already present
33+
// in hashmap it means there is a cycle
34+
// (Because you will be encountering the
35+
// node for the second time).
36+
if (s.find(h) != s.end())
37+
return true;
38+
39+
// If we are seeing the node for
40+
// the first time, insert it in hash
41+
s.insert(h);
42+
43+
h = h->next;
44+
}
45+
46+
return false;
47+
}
48+
49+
/* Driver program to test above function*/
50+
int main()
51+
{
52+
/* Start with the empty list */
53+
struct Node* head = NULL;
54+
55+
push(&head, 20);
56+
push(&head, 4);
57+
push(&head, 15);
58+
push(&head, 10);
59+
60+
/* Create a loop for testing */
61+
head->next->next->next->next = head;
62+
63+
if (detectLoop(head))
64+
cout << "Loop found";
65+
else
66+
cout << "No Loop";
67+
68+
return 0;
69+
}
70+
// This code is contributed by Geetanjali
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
//SOLUTION
3+
class Solution
4+
{
5+
6+
7+
public:
8+
9+
int maxLen (vector < int >&A, int n)
10+
11+
{
12+
13+
14+
int maxi = 0;
15+
16+
17+
int sum = 0;
18+
19+
20+
unordered_map < int, int >m;
21+
22+
23+
for (int i = 0; i < n; i++)
24+
25+
26+
{
27+
28+
29+
sum += A[i];
30+
31+
32+
if (sum == 0)
33+
34+
35+
{
36+
37+
38+
maxi = i + 1;
39+
40+
41+
}
42+
43+
44+
else
45+
46+
47+
{
48+
49+
50+
if (m.find (sum) != m.end ())
51+
52+
53+
{
54+
55+
56+
maxi = max (maxi, i - m[sum]);
57+
58+
59+
}
60+
61+
62+
else
63+
64+
65+
{
66+
67+
68+
m[sum] = i;
69+
70+
71+
}
72+
73+
74+
}
75+
76+
77+
}
78+
79+
80+
return maxi;
81+
82+
83+
}
84+
85+
86+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Given an array having both positive and negative integers. The task is to compute the length of the largest subarray with sum 0.
2+
3+
Example 1:
4+
5+
Input:
6+
N = 8
7+
A[] = {15,-2,2,-8,1,7,10,23}
8+
Output: 5
9+
Explanation: The largest subarray with
10+
sum 0 will be -2 2 -8 1 7.
11+
Your Task:
12+
You just have to complete the function maxLen() which takes two arguments an array A and n, where n is the size of the array A and returns the length of the largest subarray with 0 sum.
13+
14+
Expected Time Complexity: O(N).
15+
Expected Auxiliary Space: O(N).
16+
17+
Constraints:
18+
1 <= N <= 105
19+
-1000 <= A[i] <= 1000, for each valid i

0 commit comments

Comments
 (0)