-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet2046.cpp
More file actions
35 lines (29 loc) · 778 Bytes
/
leet2046.cpp
File metadata and controls
35 lines (29 loc) · 778 Bytes
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
class Solution {
public:
Node* sortedInsert(Node* head, int data) {
Node*tail=head;
while(tail->next!=head) tail=tail->next;
if(data<head->data){
Node*node=new Node(data);
node->next=head;
tail->next=node;
return node;
}
if(data>tail->data){
Node*node=new Node(data);
tail->next=node;
node->next=head;
return head;
}
Node*temp1=head;
Node*temp2=NULL;
while(temp1->data<data){
temp2=temp1;
temp1=temp1->next;
}
Node*node=new Node(data);
temp2->next=node;
node->next=temp1;
return head;
}
};