forked from laviii123/Btecky
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kushalsqud97
37 lines (31 loc) · 901 Bytes
/
kushalsqud97
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
/// hack swaq lavii helper
class Solution{
public:
// Should return head of the modified linked list
Node *sortedInsert(struct Node* head, int data) {
Node* curr = head;
Node* prev = NULL;
Node* newNode = new Node(data);
while(curr != NULL){
if(curr->data <= data){
prev = curr;
curr = curr->next;
}
else {
if(prev == NULL){
newNode->next = curr;
head = newNode;
} else{
newNode->next = prev->next;
prev->next = newNode;
}
break;
}
}
if(prev != NULL && prev->next == NULL){
prev->next = newNode;
}
return head;
}
};
//CPP - @geeksforgeeks_potd