From e2a61045ea26c31f3994f3eaaff7e522fa335ba4 Mon Sep 17 00:00:00 2001 From: Udhay <72250606+Udhay-Brahmi@users.noreply.github.com> Date: Tue, 15 Dec 2020 07:57:16 +0530 Subject: [PATCH] Create Absolute List Sorting --- Absolute List Sorting | 97 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 Absolute List Sorting diff --git a/Absolute List Sorting b/Absolute List Sorting new file mode 100644 index 000000000..277b2ad5a --- /dev/null +++ b/Absolute List Sorting @@ -0,0 +1,97 @@ +// { Driver Code Starts +#include +using namespace std; + +// Linked List Node +struct Node +{ + Node* next; + int data; +}; +void sortList(Node** head); +// Utility function to insert a node at the +// beginning +void push(Node** head, int data) +{ + Node* newNode = new Node; + newNode->next = (*head); + newNode->data = data; + (*head) = newNode; +} + +// Utility function to print a linked list +void printList(Node* head) +{ + while (head != NULL) + { + cout << head->data; + if (head->next != NULL) + cout << " "; + head = head->next; + } + cout<>t; +while(t--) +{ + Node* head = NULL; + cin>>n; + int arr[n]; + for(int i=0;i>arr[i]; + // push(&head, a); + } + for(int i=n-1;i>=0;i--) + { + push(&head, arr[i]); + } + // printList(head); + + sortList(&head); + + printList(head); + +} + return 0; +} +// } Driver Code Ends + + +/* The structure of the Linked list Node is as follows: +struct Node +{ + Node* next; + int data; +}; */ + +/*Your method shouldn't print anything + it should transform the passed linked list */ +void sortList(Node** head) +{ + // Your Code Here + vector vec; + Node* temp =new Node; + Node* send =new Node; + temp= (*head); + send =(*head); + while(*head!=NULL){ + vec.push_back((*head)->data); + (*head)=(*head)->next; + } + sort(vec.begin(),vec.end()); + int i=0; + while(temp!=NULL){ + temp->data = vec[i]; + i++; + temp=temp->next; + } + (*head) = send;