题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param head ListNode类 the head node
* @return ListNode类
*/
int compare(const void* a , const void* b){
int* pa = (int*)a;
int* pb = (int*)b;
return *pa - *pb;
}
struct ListNode* sortInList(struct ListNode* head ) {
// write code here
if(head == NULL || head->next == NULL) return head;
int length = 0;
struct ListNode* H = head;
while(H){
length++;
H = H->next;
}
int* num = (int*)malloc(sizeof(int)*length);
int i = 0;
H = head;
while(H){
num[i++] = H->val;
H = H->next;
}
qsort(num , length , sizeof(int) , compare);
H = head;
i = 0;
while(H){
H->val = num[i++];
H = H->next;
}
return head;
}


爱玛科技公司福利 8人发布