forked from SR-Sunny-Raj/Hacktoberfest2022-for-everyone
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request SR-Sunny-Raj#92 from heysaiyad/master
Added some problems and solution in DSA
- Loading branch information
Showing
10 changed files
with
589 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <bits/stdc++.h> | ||
|
||
using namespace std; | ||
|
||
|
||
struct Node{ | ||
int data; | ||
struct Node* next; | ||
Node(int x){ | ||
data=x; | ||
next=NULL; | ||
} | ||
}; | ||
|
||
// the helper function is a recursive function. | ||
// it works recursively until the right pointer reaches the end node. | ||
// and at each point checks it with the corresponding node from the beginning -> left pointer | ||
|
||
bool helper(Node* right, Node* &left){ | ||
if(right==NULL) return true; | ||
bool ans = helper(right->next,left) && (right->data==left->data); | ||
left=left->next; | ||
return ans; | ||
} | ||
|
||
|
||
bool isPalindrome(Node *head) | ||
{ | ||
Node* left=head; | ||
return helper(head,left); | ||
} | ||
|
||
|
||
|
||
// function to print linked list | ||
void printList(struct Node* head){ | ||
Node* temp=head; | ||
while(temp!=NULL){ | ||
cout<<temp->data<<" "; | ||
temp=temp->next; | ||
} | ||
} | ||
|
||
|
||
|
||
int main(){ | ||
|
||
// take the linked ist as input | ||
Node* head=NULL; | ||
Node* tail=NULL; | ||
|
||
int n; | ||
cin>>n; | ||
|
||
int inp; | ||
cin>>inp; | ||
head= new Node(inp); | ||
tail=head; | ||
for(int i=1;i<n;i++){ | ||
cin>>inp; | ||
tail->next=new Node(inp); | ||
tail=tail->next; | ||
} | ||
|
||
// function to print the linked list. | ||
// printList(head); | ||
// cout<<"\n"; | ||
|
||
if(isPalindrome(head)){ | ||
cout<<"The given linked list is a Palindrome"; | ||
} | ||
else{ | ||
cout<<"The given linked list is not a Palindrome"; | ||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <iostream> | ||
using namespace std; | ||
struct Node { | ||
int data; | ||
struct Node *next; | ||
}; | ||
struct Node* head = NULL; | ||
void insert(int newdata) { | ||
struct Node *newnode = (struct Node *)malloc(sizeof(struct Node)); | ||
struct Node *ptr = head; | ||
newnode->data = newdata; | ||
newnode->next = head; | ||
if (head!= NULL) { | ||
while (ptr->next != head) | ||
ptr = ptr->next; | ||
ptr->next = newnode; | ||
} else | ||
newnode->next = newnode; | ||
head = newnode; | ||
} | ||
void display() { | ||
struct Node* ptr; | ||
ptr = head; | ||
do { | ||
cout<<ptr->data <<" "; | ||
ptr = ptr->next; | ||
} while(ptr != head); | ||
} | ||
int main() { | ||
insert(3); | ||
insert(1); | ||
insert(7); | ||
insert(2); | ||
insert(9); | ||
cout<<"The circular linked list is: "; | ||
display(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Node { | ||
public: | ||
int data; | ||
Node* next; | ||
}; | ||
|
||
void push(Node** head_ref, int new_data) | ||
{ | ||
Node* new_node = new Node(); | ||
new_node->data = new_data; | ||
new_node->next = (*head_ref); | ||
(*head_ref) = new_node; | ||
} | ||
|
||
int detectLoop(Node* list) | ||
{ | ||
Node *slow_p = list, *fast_p = list; | ||
|
||
while (slow_p && fast_p && fast_p->next) { | ||
slow_p = slow_p->next; | ||
fast_p = fast_p->next->next; | ||
if (slow_p == fast_p) { | ||
return 1; | ||
} | ||
} | ||
return 0; | ||
} | ||
|
||
int main() | ||
{ | ||
Node* head = NULL; | ||
|
||
push(&head, 20); | ||
push(&head, 4); | ||
push(&head, 15); | ||
push(&head, 10); | ||
|
||
head->next->next->next->next = head; | ||
if (detectLoop(head)) | ||
cout << "Loop found"; | ||
else | ||
cout << "No Loop"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/* Link list node */ | ||
struct Node { | ||
int data; | ||
struct Node* next; | ||
}; | ||
|
||
void push(struct Node** head_ref, int new_data) | ||
{ | ||
/* allocate node */ | ||
struct Node* new_node = new Node; | ||
|
||
/* put in the data */ | ||
new_node->data = new_data; | ||
|
||
/* link the old list off the new node */ | ||
new_node->next = (*head_ref); | ||
|
||
/* move the head to point to the new node */ | ||
(*head_ref) = new_node; | ||
} | ||
|
||
// Returns true if there is a loop in linked list | ||
// else returns false. | ||
bool detectLoop(struct Node* h) | ||
{ | ||
unordered_set<Node*> s; | ||
while (h != NULL) { | ||
// If this node is already present | ||
// in hashmap it means there is a cycle | ||
// (Because you we encountering the | ||
// node for the second time). | ||
if (s.find(h) != s.end()) | ||
return true; | ||
|
||
// If we are seeing the node for | ||
// the first time, insert it in hash | ||
s.insert(h); | ||
|
||
h = h->next; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/* Driver program to test above function*/ | ||
int main() | ||
{ | ||
/* Start with the empty list */ | ||
struct Node* head = NULL; | ||
|
||
push(&head, 20); | ||
push(&head, 4); | ||
push(&head, 15); | ||
push(&head, 10); | ||
|
||
/* Create a loop for testing */ | ||
head->next->next->next->next = head; | ||
|
||
if (detectLoop(head)) | ||
cout << "Loop found"; | ||
else | ||
cout << "No Loop"; | ||
|
||
return 0; | ||
} |
49 changes: 49 additions & 0 deletions
49
Coders/DSA Concepts/Detect_Loop_in_Linked_List_Without_Floyd_Algorithm.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
class Node{ | ||
public: | ||
int data; | ||
Node *next; | ||
int visited; | ||
}; | ||
|
||
void push(Node **head_ref, int val) | ||
{ | ||
Node *newNode = new Node(); | ||
newNode->data = val; | ||
newNode->next = *head_ref; | ||
*head_ref = newNode; | ||
} | ||
|
||
bool detectLoop(Node *head) | ||
{ | ||
Node *ptr = head; | ||
while(ptr) | ||
{ | ||
if(ptr->visited == 1) return true; | ||
ptr->visited = 1; | ||
ptr = ptr->next; | ||
} | ||
return false; | ||
} | ||
|
||
int main() | ||
{ | ||
Node *head = NULL; | ||
|
||
push(&head, 10); | ||
push(&head, 9); | ||
push(&head, 8); | ||
push(&head, 7); | ||
push(&head, 6); | ||
push(&head, 5); | ||
push(&head, 4); | ||
push(&head, 3); | ||
|
||
// head->next->next->next->next->next = head; | ||
if(detectLoop(head)) cout << "Loop is present in the List\n"; | ||
else cout << "No Loop is present\n"; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
/* Link list node */ | ||
typedef struct Node { | ||
int data; | ||
struct Node* next; | ||
} SLL; | ||
|
||
/* Function to get the middle node in the linked list*/ | ||
void findMiddle(SLL *head) | ||
{ | ||
/* fast pointer moves two times fast as compared to slow pointer | ||
slow pointer iterates through each node but fast pointer skips every other node | ||
when the fast pointer reaches to the end then the slow pointer will be at the middle node | ||
*/ | ||
SLL *slow = head; | ||
SLL *fast = head; | ||
|
||
// iterating slow and fast pointers until fast becomes null or fast->next becomes null | ||
while(fast != NULL && fast->next != NULL) | ||
{ | ||
slow = slow->next; | ||
fast = fast->next->next; | ||
} | ||
|
||
printf("Middle node data : %d\n", slow->data); | ||
} | ||
|
||
void push(SLL** head_ref, int new_data) | ||
{ | ||
/* allocate node */ | ||
SLL* new_node = new Node(); | ||
|
||
/* put in the data */ | ||
new_node->data = new_data; | ||
|
||
/* link the old list off the new node */ | ||
new_node->next = (*head_ref); | ||
|
||
/* move the head to point to the new node */ | ||
(*head_ref) = new_node; | ||
} | ||
|
||
// Driver Code | ||
int main() | ||
{ | ||
/* Start with the empty list */ | ||
SLL* head = NULL; | ||
|
||
// create linked 20->4->15->35->10->44->91->2->31 | ||
push(&head, 20); | ||
push(&head, 4); | ||
push(&head, 15); | ||
push(&head, 35); | ||
push(&head, 10); // Middle Node | ||
push(&head, 44); | ||
push(&head, 91); | ||
push(&head, 2); | ||
push(&head, 31); | ||
|
||
findMiddle(head); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* Definition for singly-linked list. | ||
* struct ListNode { | ||
* int val; | ||
* ListNode *next; | ||
* ListNode() : val(0), next(nullptr) {} | ||
* ListNode(int x) : val(x), next(nullptr) {} | ||
* ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { | ||
int sum=0,carry=0; | ||
ListNode* ans=NULL,*res=NULL; | ||
while(l1 or l2 or carry) | ||
{ | ||
sum=0; | ||
if(l1) | ||
{ | ||
sum+=l1->val; | ||
l1=l1->next; | ||
} | ||
if(l2) | ||
{ | ||
sum+=l2->val; | ||
l2=l2->next; | ||
} | ||
sum+=carry; | ||
carry=sum/10; | ||
if(!ans) | ||
{ | ||
ans= new ListNode(sum%10); | ||
res=ans; | ||
} | ||
else | ||
{ | ||
ans->next= new ListNode(sum%10); | ||
ans=ans->next; | ||
} | ||
} | ||
return res; | ||
|
||
} | ||
}; |
Oops, something went wrong.