1.题目要求:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int compare(const void* a,const void* b)
{
return (*(int*)a - *(int*)b);
}
struct ListNode* trainningPlan(struct ListNode* l1, struct ListNode* l2) {
if(l1 == NULL&&l2 == NULL)
return NULL;
if(l1 == NULL)
return l2;
if(l2 == NULL)
return l1;
struct ListNode* cur = l1;
struct ListNode* cur1 = l2;
int count = 0;
while(cur)
{
count++;
cur = cur->next;
}
while(cur1)
{
count++;
cur1 = cur1->next;
}
//创建数组
int* number = (int*)malloc(sizeof(int) * count);
int f = 0;
cur = l1;
cur1 = l2;
//往数组里存值
while(cur)
{
number[f] = cur->val;
f++;
cur = cur->next;
}
while(cur1)
{
number[f] = cur1->val;
f++;
cur1 = cur1->next;
}
//给数组排序
qsort(number,count,sizeof(int),compare);
cur = l1;
cur1 = l2;
//连接两个链表
while(cur->next != NULL)
{
cur = cur->next;
}
f = 0;
cur->next = l2;
cur = l1;
//把数组的值存入链表中
while(cur)
{
cur->val = number[f];
f++;
cur = cur->next;
}
return l1;
}