-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertion Sort for Singly Linked List.cpp
111 lines (91 loc) · 2.47 KB
/
Insertion Sort for Singly Linked List.cpp
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//{ Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
/* Link list node */
struct Node
{
int data;
struct Node *next;
Node(int x)
{
data = x;
next = NULL;
}
};
/* Function to print linked list */
void printList(struct Node *head)
{
struct Node *temp = head;
while (temp != NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
// } Driver Code Ends
// User function Template for C++
/*Link list node
struct Node {
int data;
struct Node *next;
Node(int x) {
data = x;
next = NULL;
}
};*/
class Solution
{
public:
Node *insertionSort(struct Node *head_ref)
{
Node *res = new Node(-1); // Create a new list to serve as the result
Node *temp = res; // Create a temporary list to traverse the result list
Node *curr = head_ref; // Create a current list to traverse the original list
while (curr) // Iterate through the original list
{
temp = res; // Reset the temporary list to the beginning of the result list for each iteration
// Find the correct position to insert the current node in the result list
while (temp->next && temp->next->data <= curr->data)
{
temp = temp->next;
}
Node *nextNode = temp->next; // Temporarily store the second part of the result list in order to connect it later with the first part.
temp->next = curr; // Insert the current node at the correct position in the sorted list (result list)
curr = curr->next; // Move to the next node in the current list
temp->next->next = nextNode; // Connect the first part of the result list with the second part
}
return res->next; // Return the sorted result list
}
};
//{ Driver Code Starts.
int main()
{
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
Node *head = NULL;
Node *temp = head;
for (int i = 0; i < n; i++)
{
int data;
cin >> data;
if (head == NULL)
head = temp = new Node(data);
else
{
temp->next = new Node(data);
temp = temp->next;
}
}
Solution ob;
head = ob.insertionSort(head);
printList(head);
cout << "\n";
}
return 0;
}
// } Driver Code Ends