From ba659118a8ab6fa88a18cc2ad229a6fab5a63363 Mon Sep 17 00:00:00 2001 From: pallavipriya4321 <56637207+pallavipriya4321@users.noreply.github.com> Date: Tue, 5 Oct 2021 23:40:34 +0530 Subject: [PATCH] Delete a node from middle of linked list --- Delete_middle_of_a_linked_lists.cpp | 108 ++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 Delete_middle_of_a_linked_lists.cpp diff --git a/Delete_middle_of_a_linked_lists.cpp b/Delete_middle_of_a_linked_lists.cpp new file mode 100644 index 0000000..8dc7567 --- /dev/null +++ b/Delete_middle_of_a_linked_lists.cpp @@ -0,0 +1,108 @@ +#include +using namespace std; + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + +/* Function to get the middle of the linked list*/ +struct Node *deleteMid(struct Node *head); +void printList(Node* node) +{ + while (node != NULL) { + cout << node->data <<" "; + node = node->next; + } + cout<<"\n"; +} +int main() +{ + int t; + cin>>t; + while(t--) + { + int n; + cin>>n; + + int data; + cin>>data; + struct Node *head = new Node(data); + struct Node *tail = head; + for (int i = 0; i < n-1; ++i) + { + cin>>data; + tail->next = new Node(data); + tail = tail->next; + } + head = deleteMid(head); + printList(head); + } + return 0; +} + + +// } Driver Code Ends + + + +/* Link list Node: + +struct Node +{ + int data; + struct Node* next; + + Node(int x){ + data = x; + next = NULL; + } +}; + +*/ + +// Deletes middle of linked list and returns head of the modified list +Node* deleteMid(Node* head) +{ + // Your Code Here + struct Node *ptr=head,*pre; + int c=0; + while(ptr!=NULL) + { + c++; + ptr=ptr->next; + } + if(head==NULL || c==1) + return NULL; + + if(c%2==1) + c=(c+1)/2; + else + c=(c/2)+1; + ptr=head; + pre=ptr; + int i=0; + while(1) + { + i++; + if(i==c) + { + pre->next=ptr->next; + free(ptr); + break; + } + else + { + pre=ptr; + ptr=ptr->next; + } + } + return head; + +} \ No newline at end of file