From 339f80a84b5c70c3ff2c78ee2654a0a44f3e672e Mon Sep 17 00:00:00 2001 From: Bhavanak021 Date: Fri, 1 Oct 2021 16:50:39 +0530 Subject: [PATCH 1/4] Flatten-Linked-List --- C++/Flatten-Linked-List.cpp | 144 ++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 C++/Flatten-Linked-List.cpp diff --git a/C++/Flatten-Linked-List.cpp b/C++/Flatten-Linked-List.cpp new file mode 100644 index 00000000..f7498e68 --- /dev/null +++ b/C++/Flatten-Linked-List.cpp @@ -0,0 +1,144 @@ +/* +Flattening a Linked List +Medium Accuracy: 33.91% Submissions: 67762 Points: 4 +Given a Linked List of size N, where every node represents a sub-linked-list and contains two pointers: +(i) a next pointer to the next node, +(ii) a bottom pointer to a linked list where this node is head. +Each of the sub-linked-list is in sorted order. +Flatten the Link List such that all the nodes appear in a single level while maintaining the sorted order. +Note: The flattened list will be printed using the bottom pointer instead of next pointer. + +Example 1: +Input: +5 -> 10 -> 19 -> 28 +| | | | +7 20 22 35 +| | | +8 50 40 +| | +30 45 +Output: 5-> 7-> 8- > 10 -> 19-> 20-> +22-> 28-> 30-> 35-> 40-> 45-> 50. +Explanation: +The resultant linked lists has every +node in a single level. +(Note: | represents the bottom pointer.) + +Example 2: +Input: +5 -> 10 -> 19 -> 28 +| | +7 22 +| | +8 50 +| +30 +Output: 5->7->8->10->19->20->22->30->50 +Explanation: +The resultant linked lists has every +node in a single level. +(Note: | represents the bottom pointer.) +*/ +#include +using namespace std; +class Node +{ + public: + int data; + Node *right, *down; +}; + +Node* head ; + +Node* merge(Node* a, Node* b) +{ + if (a == NULL) + return b; + if (b == NULL) + return a; + Node* result; + + if (a->data < b->data) + { + result = a; + result->down = merge(a->down, b); + } + else + { + result = b; + result->down = merge(a, b->down); + } + result->right = NULL; + return result; +} + +Node* flatten(Node* root) +{ + if (root == NULL || root->right == NULL) + return root; + return merge(root, flatten(root->right)); +} + +Node* push(Node* head_ref, int data) +{ + + Node* new_node = new Node(); + + new_node->data = data; + new_node->right = NULL; + new_node->down = head_ref; + head_ref = new_node; + + return head_ref; +} + +void printList() +{ + Node* temp = head; + while (temp != NULL) + { + cout << temp->data << " "; + temp = temp->down; + } + cout << endl; +} + +// Driver code +int main() +{ + + /* Let us create the following linked list + 5 -> 10 -> 19 -> 28 + | | | | + V V V V + 7 20 22 35 + | | | + V V V + 8 50 40 + | | + V V + 30 45 + */ + head = push(head, 30); + head = push(head, 8); + head = push(head, 7); + head = push(head, 5); + + head->right = push(head->right, 20); + head->right = push(head->right, 10); + + head->right->right = push(head->right->right, 50); + head->right->right = push(head->right->right, 22); + head->right->right = push(head->right->right, 19); + + head->right->right->right = push(head->right->right->right, 45); + head->right->right->right = push(head->right->right->right, 40); + head->right->right->right = push(head->right->right->right, 35); + head->right->right->right = push(head->right->right->right, 20); + + // Flatten the list + head = flatten(head); + + printList(); + return 0; +} \ No newline at end of file From c26d7f61afc3094534da337dc49c521ba50a5907 Mon Sep 17 00:00:00 2001 From: Bhavanak021 Date: Fri, 1 Oct 2021 16:53:30 +0530 Subject: [PATCH 2/4] Detect loop in a linked list --- C++/Detect-Loop-in-Linked-List.cpp | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 C++/Detect-Loop-in-Linked-List.cpp diff --git a/C++/Detect-Loop-in-Linked-List.cpp b/C++/Detect-Loop-in-Linked-List.cpp new file mode 100644 index 00000000..1c846991 --- /dev/null +++ b/C++/Detect-Loop-in-Linked-List.cpp @@ -0,0 +1,74 @@ +/*Detect Loop in linked list +Given a linked list of N nodes. The task is to check if the linked list has a loop. Linked list can contain self loop. + +Example 1: + +Input: +N = 3 +value[] = {1,3,4} +x = 2 +Output: True +Explanation: In above test case N = 3. +The linked list with nodes N = 3 is +given. Then value of x=2 is given which +means last node is connected with xth +node of linked list. Therefore, there +exists a loop. +Example 2: + +Input: +N = 4 +value[] = {1,8,3,4} +x = 0 +Output: False +Explanation: For N = 4 ,x = 0 means +then lastNode->next = NULL, then +the Linked list does not contains +any loop.*/ + +#include +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; +} \ No newline at end of file From fed7571110e955a6b043eea7e85af95bc9116cb0 Mon Sep 17 00:00:00 2001 From: Bhavanak021 Date: Fri, 1 Oct 2021 16:56:24 +0530 Subject: [PATCH 3/4] To check if linked list is pallindrome or not --- C++/Linked-list-is-pallindrome-or-not.cpp | 94 +++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 C++/Linked-list-is-pallindrome-or-not.cpp diff --git a/C++/Linked-list-is-pallindrome-or-not.cpp b/C++/Linked-list-is-pallindrome-or-not.cpp new file mode 100644 index 00000000..eb907580 --- /dev/null +++ b/C++/Linked-list-is-pallindrome-or-not.cpp @@ -0,0 +1,94 @@ +/*Check if Linked List is Palindrome +Given a singly linked list of size N of integers. The task is to check if the given linked list is palindrome or not. + +Example 1: + +Input: +N = 3 +value[] = {1,2,1} +Output: 1 +Explanation: The given linked list is +1 2 1 , which is a palindrome and +Hence, the output is 1. +Example 2: + +Input: +N = 4 +value[] = {1,2,3,4} +Output: 0 +Explanation: The given linked list +is 1 2 3 4 , which is not a palindrome +and Hence, the output is 0.*/ +#include +#include +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 countSum(Node* slow){ + int res=1; + Node* temp = slow; + while(temp->next!=slow){ + res++; + temp = temp->next; + } + return res; +} + +bool isPalindrome(Node *head) + { + Node* curr= head; + int count=0, pos; + stack v; + while(curr!=NULL){ + count++; + v.push(curr->data); + curr = curr->next; + } + curr = head; + while(head!=NULL){ + // cout<<"head: "<data<<" top: "<data != v.top()) + return 0; + head = head->next; + v.pop(); + } + + return 1; + } +void printList(struct Node* node) +{ + // Print the list after loop removal + while (node != NULL) { + cout << node->data << " "; + node = node->next; + } +} + +int main() +{ + Node* head = NULL; + + push(&head, 1); + push(&head, 2); + push(&head, 1); + // printList(head); + if(isPalindrome(head)){ + cout<<"It is a pallindrome"; + }else{ + cout<<"It is not a pallindrome"; + } + +} \ No newline at end of file From 15b67dcfa9252f445b2c4b20dfca20e4c8794568 Mon Sep 17 00:00:00 2001 From: Bhavanak021 Date: Fri, 1 Oct 2021 16:58:49 +0530 Subject: [PATCH 4/4] Merge sort of a linked list --- C++/Merge-sort-Linked-List.cpp | 117 +++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 C++/Merge-sort-Linked-List.cpp diff --git a/C++/Merge-sort-Linked-List.cpp b/C++/Merge-sort-Linked-List.cpp new file mode 100644 index 00000000..6a312e3e --- /dev/null +++ b/C++/Merge-sort-Linked-List.cpp @@ -0,0 +1,117 @@ +/*Merge Sort for Linked List +Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort. +Note: If the length of linked list is odd, then the extra node should go in the first list while splitting. + +Example 1: + +Input: +N = 5 +value[] = {3,5,2,4,1} +Output: 1 2 3 4 5 +Explanation: After sorting the given +linked list, the resultant matrix +will be 1->2->3->4->5. +Example 2: + +Input: +N = 3 +value[] = {9,15,0} +Output: 0 9 15 +Explanation: After sorting the given +linked list , resultant will be +0->9->15. +Expected Time Complexity: O(N*Log(N)) +Expected Auxiliary Space: O(N) + */ +#include +using namespace std; +struct node{ + int data; + node* next; +}; +void push(node** head, int l_data){ + node* temp = new node(); + temp->data = l_data; + temp->next=NULL; + if(*head == NULL){ + *head = temp; + return; + } + node*n = *head; + while(n->next!=NULL) + n = n->next; + n->next=temp; +} +void findMiddleElement(node* curr, node**first, node**second){ + node* slow= curr; + node* fast = curr->next; + while(fast!=NULL){ + fast=fast->next; + while(fast!=NULL){ + slow=slow->next; + fast=fast->next; + } + } + + *first = curr; + *second = slow->next; + slow->next=NULL; +} + +node* mergeBoth(node* first, node* second){ + node* answer = NULL; + if(!first) + return second; + if(!second) + return first; + + if(first->data <= second->data){ + answer = first; + answer->next = mergeBoth(first->next, second); + }else{ + answer = second; + answer->next = mergeBoth(first, second->next); + } + return answer; +} + +void MergeSorting(node**head){ + node* curr= *head; + node* first; + node* second; + if(!curr || !curr->next) return; + + findMiddleElement(curr, &first, &second); + + MergeSorting(&first); + MergeSorting(&second); + *head = mergeBoth(first, second); +} +node* mergeSort(node* head){ + MergeSorting(&head); + return head; +} +void print(node* n){ + while(n!=NULL){ + cout<data<<" "; + n = n->next; + } + cout<<"\n"; +} +int main(){ + node* head = NULL; + int n, ele; + cout<<"\n Enter the size of the llist : "; + cin>>n; + cout<<"\n Enter elements to be entered in linked list: "; + for(int i=0; i>ele; + push(&head, ele); + } + cout<<"\n The llist 1 is: "; + print(head); + head = mergeSort(head); + cout<<"\n merge sort of ll: "; + + print(head); +} \ No newline at end of file