diff --git a/Linked_List/Detect_cycle_in_LinkedList.cpp b/Linked_List/Detect_cycle_in_LinkedList.cpp new file mode 100644 index 0000000..d47d973 --- /dev/null +++ b/Linked_List/Detect_cycle_in_LinkedList.cpp @@ -0,0 +1,91 @@ + +#include +using namespace std; + +struct Node +{ + int data; + Node* next; + + Node(int val) + { + data = val; + next = NULL; + } +}; + +void loopHere(Node* head, Node* tail, int position) +{ + if(position==0) return; + + Node* walk = head; + for(int i=1; inext; + tail->next = walk; +} + + +class Solution +{ + public: + //Function to check if the linked list has a loop. + bool detectLoop(Node* head) + + { + + Node *slow=head,*fast=head; + + while(fast!=NULL && fast->next!=NULL){ + + slow=slow->next; + + fast=fast->next->next; + + + + if(slow==fast) + + return true; + + } + + return false; + + } +}; + + + +int main() +{ + int t; + cin>>t; + while(t--) + { + int n, num; + cin>>n; + + Node *head, *tail; + cin>> num; + head = tail = new Node(num); + + for(int i=0 ; i> num; + tail->next = new Node(num); + tail = tail->next; + } + + int pos; + cin>> pos; + loopHere(head,tail,pos); + + Solution ob; + if(ob.detectLoop(head) ) + cout<< "True\n"; + else + cout<< "False\n"; + } + return 0; +} + diff --git a/Linked_List/Flattening_a_LinkedList.cpp b/Linked_List/Flattening_a_LinkedList.cpp new file mode 100644 index 0000000..7e28428 --- /dev/null +++ b/Linked_List/Flattening_a_LinkedList.cpp @@ -0,0 +1,84 @@ +// C++ code for the above approach +#include +using namespace std; + +// Linked list Node +struct Node { + int data; + struct Node* next; + struct Node* bottom; + + Node(int x) + { + data = x; + next = NULL; + bottom = NULL; + } +}; + +// comparator function for priority queue +struct mycomp { + bool operator()(Node* a, Node* b) + { + return a->data > b->data; + } +}; + +void flatten(Node* root) +{ + priority_queue, mycomp> p; + // pushing main link nodes into priority_queue. + while (root != NULL) { + p.push(root); + root = root->next; + } + + // Extracting the minimum node + // while priority queue is not empty + while (!p.empty()) { + + // extracting min + auto k = p.top(); + p.pop(); + + // printing least element + cout << k->data << " "; + if (k->bottom) + p.push(k->bottom); + } +} + +// Driver's code +int main(void) +{ + // This code builds the flattened linked list + // of first picture in this article ; + Node* head = new Node(5); + auto temp = head; + auto bt = head; + bt->bottom = new Node(7); + bt->bottom->bottom = new Node(8); + bt->bottom->bottom->bottom = new Node(30); + temp->next = new Node(10); + + temp = temp->next; + bt = temp; + bt->bottom = new Node(20); + temp->next = new Node(19); + temp = temp->next; + bt = temp; + bt->bottom = new Node(22); + bt->bottom->bottom = new Node(50); + temp->next = new Node(28); + temp = temp->next; + bt = temp; + bt->bottom = new Node(35); + bt->bottom->bottom = new Node(40); + bt->bottom->bottom->bottom = new Node(45); + + // Function call + flatten(head); + cout << endl; + return 0; +} +// this code is contributed by user_990i diff --git a/Linked_List/Middle_of_LinkedList.cpp b/Linked_List/Middle_of_LinkedList.cpp new file mode 100644 index 0000000..e04340e --- /dev/null +++ b/Linked_List/Middle_of_LinkedList.cpp @@ -0,0 +1,83 @@ +#include +using namespace std; + +// Link list node +struct node +{ + int data; + struct node* next; +}; + +// Function to get the middle of +// the linked list +void printMiddle(struct node* head) +{ + int count = 0; + struct node* mid = head; + + while (head != NULL) + { + + // Update mid, when 'count' + // is odd number + if (count & 1) + mid = mid->next; + + ++count; + head = head->next; + } + + // If empty list is provided + if (mid != NULL) + printf("The middle element is [%d]\n\n", + mid->data); +} + +void push(struct node** head_ref, int new_data) +{ + + // Allocate node + struct node* new_node = (struct node*)malloc( + sizeof(struct 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; +} + +// A utility function to print +// a given linked list +void printList(struct node* ptr) +{ + while (ptr != NULL) + { + printf("%d->", ptr->data); + ptr = ptr->next; + } + printf("NULL\n"); +} + +// Driver code +int main() +{ + + // Start with the empty list + struct node* head = NULL; + int i; + + for(i = 5; i > 0; i--) + { + push(&head, i); + printList(head); + printMiddle(head); + } + return 0; +} + +// This code is contributed by ac121102 diff --git a/Linked_List/Nth node from end.cpp b/Linked_List/Nth node from end.cpp new file mode 100644 index 0000000..7fab9d4 --- /dev/null +++ b/Linked_List/Nth node from end.cpp @@ -0,0 +1,89 @@ +//{ Driver Code Starts +// C program to find n'th Node in linked list +#include +#include +#include +using namespace std; + +/* Link list Node */ +struct Node { + int data; + struct Node *next; + Node(int x) { + data = x; + next = NULL; + } +}; + + +/* Function to get the nth node from the last of a linked list*/ +int getNthFromLast(struct Node* head, int n); + + + +/* Driver program to test above function*/ +int main() +{ + int T,i,n,l,k; + + cin>>T; + + while(T--){ + struct Node *head = NULL, *tail = NULL; + + cin>>n>>k; + int firstdata; + cin>>firstdata; + head = new Node(firstdata); + tail = head; + for(i=1;i>l; + tail->next = new Node(l); + tail = tail->next; + } + + cout<next; + + cnt++; + + } + + if(cntnext; + + return temp->data; + +} +