先将链表转换为数组,排序,然后再输出
/** * struct ListNode { * int val; * struct ListNode *next; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * * @param head ListNode类 the head node * @return ListNode类 */ int cmp(const void *a,const void *b){ return *(int*)b-*(int*)a; } struct ListNode* sortInList(struct ListNode* head ) { // write code here int* num = (int*)malloc(sizeof(int)*100001); int cnt = 0; while(head){ num[cnt++] = head->val; head = head->next; } qsort(num,cnt,sizeof(int),cmp); struct ListNode* p = (struct ListNode*)malloc(sizeof(struct ListNode)); p = NULL; for(int i = 0; i < cnt;i++){ struct ListNode* t = (struct ListNode*)malloc(sizeof(struct ListNode)); t->val = num[i]; t->next = p; p = t; } return p; }