From 661d5759d0cbd439ee1fc4daab470824fc012b16 Mon Sep 17 00:00:00 2001 From: Ashish Kotyan Date: Fri, 1 Oct 2021 19:06:44 +0530 Subject: [PATCH 1/3] Two linked list program --- ...Duplicates in a Un-sorted Linked List..cpp | 85 ++++++++++++++++++ Remove Duplicates in a sorted Linked List.cpp | 87 +++++++++++++++++++ 2 files changed, 172 insertions(+) create mode 100644 Remove Duplicates in a Un-sorted Linked List..cpp create mode 100644 Remove Duplicates in a sorted Linked List.cpp diff --git a/Remove Duplicates in a Un-sorted Linked List..cpp b/Remove Duplicates in a Un-sorted Linked List..cpp new file mode 100644 index 000000000..d8255d577 --- /dev/null +++ b/Remove Duplicates in a Un-sorted Linked List..cpp @@ -0,0 +1,85 @@ +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; + +}; + +void push(Node** head, int val) +{ + + Node* new_node = new Node(); + new_node->data = val; + + new_node->next = *head; + *head = new_node; + +} + +void removeDupplicat(Node* head) +{ + Node* temp = head; + Node* ptr1; + Node* dubli; + while(temp!=NULL && temp->next!=NULL) + + { + + ptr1 = temp; + + while(ptr1->next !=NULL) + { + if(temp->data == ptr1->next->data) + { + dubli = ptr1->next; + ptr1->next = ptr1->next->next; + delete (dubli); + } + else { + ptr1 = ptr1->next; + } + } + temp = temp->next; + + } + +} + +void print(Node* head) +{ + + Node* temp = head; + + while(temp!=NULL) + { + cout<data<<"->"; + temp = temp->next; + + } + cout<<"NULL"< +using namespace std; + +class Node{ + public: + int data; + Node* next; + +}; + +void push(Node** head, int val) +{ + + Node* new_node = new Node(); + new_node->data = val; + + new_node->next = *head; + *head = new_node; + + +} + +void removeDupplicat(Node* head) +{ + Node* temp = head; + Node* next = NULL; + + + while(temp!=NULL) + { + if(temp->data == temp->next->data) + { + cout<data<<" "<next->data<<" \n"; + + next = temp->next->next; + free (temp->next); + temp->next = next; + } + + temp = temp->next; + + } + + + + + +} + + + +void print(Node* head) +{ + + Node* temp = head; + + while(temp!=NULL) + { + cout<data<<"->"; + temp = temp->next; + + } + cout<<"NULL"< Date: Fri, 1 Oct 2021 19:07:56 +0530 Subject: [PATCH 2/3] total four Linked list Program --- Find the middle Element of a linked list..cpp | 62 ++++++++++++++ ...ast element to Front in a Linked List..cpp | 83 +++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 Find the middle Element of a linked list..cpp create mode 100644 Write a Program to Move the last element to Front in a Linked List..cpp diff --git a/Find the middle Element of a linked list..cpp b/Find the middle Element of a linked list..cpp new file mode 100644 index 000000000..d8c903a63 --- /dev/null +++ b/Find the middle Element of a linked list..cpp @@ -0,0 +1,62 @@ +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; + +}; +void push(Node**head, int val) +{ + + Node* new_node = new Node(); + new_node->data = val; + new_node->next = *head; + *head = new_node; +} +int lenght(Node* head) +{ + Node* tem = head; + int count =0; + while(tem!=NULL) + { +// cout<<"1"; + count++; + + tem = tem->next; + + } + return count; + + + +} +void dp(Node* head) +{ + Node* temp = head; + while(temp!=NULL) + { + cout<data<<"->"; + temp = temp->next; + } + cout<<"NULL\n"; + int mid = lenght(head); + for(int i=0;inext; + } + cout<data; +} + +int main() +{ + Node* head = NULL; + push(&head,9); + push(&head,4); + push(&head,6); + push(&head,5); + push(&head,3); + push(&head,2); + dp(head); +} diff --git a/Write a Program to Move the last element to Front in a Linked List..cpp b/Write a Program to Move the last element to Front in a Linked List..cpp new file mode 100644 index 000000000..adabaa34e --- /dev/null +++ b/Write a Program to Move the last element to Front in a Linked List..cpp @@ -0,0 +1,83 @@ +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; + +}; + +void push(Node** head, int val) +{ + + Node* new_node = new Node(); + new_node->data = val; + + new_node->next = *head; + *head = new_node; + + +} + + + + +void last_in_fron(Node** head) +{ + + + Node* prev = NULL; + Node* last = *head; + + while(last->next!=NULL) + { + prev = last; + last = last->next; + } + + prev->next= NULL; + last->next = *head; + + *head = last; + + +} + + + +void print(Node* head) +{ + + Node* temp = head; + + while(temp!=NULL) + { + cout<data<<"->"; + temp = temp->next; + + } + cout<<"NULL"< Date: Fri, 1 Oct 2021 19:09:15 +0530 Subject: [PATCH 3/3] Total 7 Linked list Question Asked by Top MNc --- Deletion from a Circular Linked List..cpp | 86 +++++++++++++ ... Circular linked list into two halves..cpp | 116 ++++++++++++++++++ ...ly Linked list is a palindrome or not..cpp | 90 ++++++++++++++ 3 files changed, 292 insertions(+) create mode 100644 Deletion from a Circular Linked List..cpp create mode 100644 Split a Circular linked list into two halves..cpp create mode 100644 check Singly Linked list is a palindrome or not..cpp diff --git a/Deletion from a Circular Linked List..cpp b/Deletion from a Circular Linked List..cpp new file mode 100644 index 000000000..2264d359d --- /dev/null +++ b/Deletion from a Circular Linked List..cpp @@ -0,0 +1,86 @@ +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; + +}; + +void push(Node** head, int val) + +{ + Node* new_node = new Node(); + new_node->data = val; + + new_node->next = *head; + *head = new_node; +} + + +void circular(Node* head) +{ + Node* temp = head; + + while(temp->next!=NULL) + { + temp = temp->next; + } + + temp->next = head; + +} + + + +void del_IN_cirlular(Node* head, int key) +{ + Node* temp = head; + Node* prev = NULL; + + while(temp->data!=key) + { + prev = temp; + temp = temp->next; + } + prev->next = temp->next; + delete temp; + temp = prev->next; + + + +} + +void print(Node* head) +{ + Node* temp = head; + do { + cout<data<<"->"; + temp = temp->next; + }while(temp!=head); + cout< +using namespace std; + +/* structure for a node */ +class Node +{ + public: + int data; + Node *next; +}; + +void splitList(Node *head, Node **head1_ref, Node **head2_ref) +{ + Node *slow_ptr = head; + Node *fast_ptr = head; + + if(head == NULL) + return; + + + while(fast_ptr->next != head && fast_ptr->next->next != head) + { + fast_ptr = fast_ptr->next->next; + slow_ptr = slow_ptr->next; + } + + cout<data<next->next == head) + fast_ptr = fast_ptr->next; + cout<next->data; + + + *head1_ref = head; + + + if(head->next != head) + *head2_ref = slow_ptr->next; + + + fast_ptr->next = slow_ptr->next; + + + slow_ptr->next = head; +} + + +void push(Node **head_ref, int data) +{ + Node *ptr1 = new Node(); + Node *temp = *head_ref; + ptr1->data = data; + ptr1->next = *head_ref; + + /* If linked list is not NULL then + set the next of last node */ + if(*head_ref != NULL) + { + while(temp->next != *head_ref) + temp = temp->next; + temp->next = ptr1; + } + else + ptr1->next = ptr1; /*For the first node */ + + *head_ref = ptr1; +} + +/* Function to print nodes in +a given Circular linked list */ +void printList(Node *head) +{ + Node *temp = head; + if(head != NULL) + { + cout << endl; + do { + cout << temp->data << " "; + temp = temp->next; + } while(temp != head); + } +} + +// Driver Code +int main() +{ + int list_size, i; + + /* Initialize lists as empty */ + Node *head = NULL; + Node *head1 = NULL; + Node *head2 = NULL; + + /* Created linked list will be 12->56->2->11 */ + push(&head, 12); + push(&head, 56); + push(&head, 2); + push(&head, 11); + + cout << "Original Circular Linked List"; + printList(head); + + /* Split the list */ + splitList(head, &head1, &head2); + + cout << "\nFirst Circular Linked List"; + printList(head1); + + cout << "\nSecond Circular Linked List"; + printList(head2); + return 0; +} + + diff --git a/check Singly Linked list is a palindrome or not..cpp b/check Singly Linked list is a palindrome or not..cpp new file mode 100644 index 000000000..2374bec2b --- /dev/null +++ b/check Singly Linked list is a palindrome or not..cpp @@ -0,0 +1,90 @@ +#include +using namespace std; + +class Node{ + public: + int data; + Node* next; + +}; + +void push(Node** head, int val) +{ + Node* new_node = new Node(); + new_node->data = val; + new_node->next = *head; + *head = new_node; + +} + + +bool Check_pali(Node* head) +{ + Node* fast = head; + Node* slow = head; + while(fast!=NULL and fast->next!=NULL) + { + slow = slow->next; + fast = fast->next->next; + } + + + Node* temp = NULL; + Node* prev = NULL; + Node* current = slow; + + while(current!=NULL) + { + temp = current->next; + current->next = prev; + prev = current; + current = temp; + } + fast = head; + while(prev!=NULL) + { + if(fast->data!=prev->data) + { + return false; + } + fast = fast->next; + prev = prev->next; + } + return true; +} + + +void print(Node* head) +{ + Node* temp = head; + while(temp!=NULL) + { + cout<data<<"->"; + temp = temp->next; + + } + cout<<"NULL\n"; + + +} + + +int main() +{ + + + Node* head = NULL; + + + push(&head,1); + push(&head,2); + push(&head,2); + push(&head,2); + push(&head,2); + push(&head,2); + push(&head,1); + print(head); + int check = Check_pali(head); + cout<