-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathAbsolute List Sorting
97 lines (86 loc) · 1.57 KB
/
Absolute List Sorting
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;