-
Notifications
You must be signed in to change notification settings - Fork 12
/
13.01.2024.cpp
53 lines (47 loc) · 1.03 KB
/
13.01.2024.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
//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* insert(Node* newNode, Node* ans){
if(ans == NULL){
newNode->next = ans;
ans = newNode;
}
else if(ans->data > newNode->data){
newNode->next = ans;
ans = newNode;
}
else{
Node *temp=ans;
while(temp->next !=NULL && temp->next->data <= newNode->data){
temp=temp->next;
}
newNode->next = temp->next;
temp->next= newNode;
}
return ans;
}
Node* insertionSort(struct Node* head)
{
//code here
if(!head || !head->next){
return head;
}
Node* ans=NULL;
while(head != NULL){
Node* next = head->next;
ans = insert(head, ans);
head = next;
}
return ans;
}
};