Skip to content

Commit

Permalink
Merge pull request SR-Sunny-Raj#92 from heysaiyad/master
Browse files Browse the repository at this point in the history
Added some problems and solution in DSA
  • Loading branch information
SR-Sunny-Raj authored Oct 9, 2022
2 parents 0606a52 + 3b17e38 commit d5170b0
Show file tree
Hide file tree
Showing 10 changed files with 589 additions and 0 deletions.
79 changes: 79 additions & 0 deletions Coders/DSA Concepts/CheckLinkedListisPalindrome.cpp
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;
}

38 changes: 38 additions & 0 deletions Coders/DSA Concepts/Circularlinkedlist.cpp
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;
}
47 changes: 47 additions & 0 deletions Coders/DSA Concepts/Detect Loop in LinkedList.cpp
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;
}
68 changes: 68 additions & 0 deletions Coders/DSA Concepts/Detect loop in a linked list.cpp
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;
}
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;
}
65 changes: 65 additions & 0 deletions Coders/DSA Concepts/Find middle node.cpp
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;
}
45 changes: 45 additions & 0 deletions Coders/DSA Concepts/add_two_numbers.cpp
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;

}
};
Loading

0 comments on commit d5170b0

Please sign in to comment.