-
Notifications
You must be signed in to change notification settings - Fork 0
/
148.sort-list.c
64 lines (58 loc) · 1.6 KB
/
148.sort-list.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* @lc app=leetcode.cn id=148 lang=c
*
* [148] Sort List
*/
#include "include/type.h"
#include <stdio.h>
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
#include <stdlib.h>
int list_node_cmp(const void *a, const void *b)
{
return ((*(*(struct ListNode **)a)).val - (*(*(struct ListNode **)b)).val);
}
struct ListNode *sortList(struct ListNode *head)
{
if (head == NULL || head->next == NULL)
{
return head;
}
struct ListNode **sorted = (struct ListNode **)malloc(sizeof(struct ListNode *) * 50000);
int sorted_size = 0;
while (head != NULL)
{
sorted[sorted_size++] = head;
head = head->next;
}
qsort(sorted, sorted_size, sizeof(struct ListNode *), list_node_cmp);
for (int i = 0; i < sorted_size - 1; i++)
{
sorted[i]->next = sorted[i + 1];
}
sorted[sorted_size - 1]->next = NULL;
return sorted[0];
}
// @lc code=end
int main(int argc, char const *argv[])
{
struct ListNode *head = (struct ListNode *)malloc(sizeof(struct ListNode));
head->val = 4;
head->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->val = 2;
head->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->next->val = 1;
head->next->next->next = (struct ListNode *)malloc(sizeof(struct ListNode));
head->next->next->next->val = 3;
head->next->next->next->next = NULL;
// head->next->next->next->next->val = 0;
// head->next->next->next->next->next = NULL;
sortList(head);
return 0;
}