-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInsertionSortList.c
More file actions
39 lines (36 loc) · 806 Bytes
/
Copy pathInsertionSortList.c
File metadata and controls
39 lines (36 loc) · 806 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
35
36
37
38
39
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insert(struct ListNode* head,struct ListNode* temp){
struct ListNode* t,*u;
u=head;
t=head->next;
if(u->val>=temp->val){
temp->next=head;
return temp;
}
while(t!=NULL && t->val<temp->val){
u=t;
t=t->next;
}
u->next=temp;
temp->next=t;
return head;
}
struct ListNode* insertionSortList(struct ListNode* head) {
struct ListNode *temp,*next;
if(head==NULL) return NULL;
temp=head->next;
head->next=NULL;
while(temp!=NULL){
next=temp->next;
temp->next=NULL;
head=insert(head,temp);
temp=next;
}
return head;
}