Skip to content

Create Absolute List Sorting #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions Absolute List Sorting
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// { Driver Code Starts
#include <bits/stdc++.h>
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<<endl;
}


// Driver code
int main()
{

int t=0;
int n = 0;
cin>>t;
while(t--)
{
Node* head = NULL;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>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<int> 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;