将这两个链表的val全部用数组储存,然后对数组进行冒泡,链表装数组数字,输出链表。
/**
- struct ListNode {
- int val;
- struct ListNode *next;
- }; */
/** *
- @param pHead1 ListNode类
- @param pHead2 ListNode类
- @return ListNode类 / struct ListNode Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {// write code here int a [2000]; int i = 0; while (pHead1) { a[i++] = pHead1->val; pHead1 = pHead1->next; } while (pHead2) { a[i++] = pHead2->val; pHead2 = pHead2->next; } for (int c = i-1; c > 0; c--) { for (int b = 0; b +1<= c; b++) { if (a[b] > a[b + 1]) { int tmp = a[b + 1]; a[b + 1] = a[b]; a[b] = tmp;} struct ListNode* tm = ( struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* tmp = ( struct ListNode*)malloc(sizeof(struct ListNode)); tm->next = tmp; tmp->next = NULL;for (int k = 0; k < i; k++) { struct ListNode* tmp1 = ( struct ListNode*)malloc(sizeof(struct ListNode)); tmp1->val = a[k]; tmp->next = tmp1; tmp = tmp->next;}return tm->next->next; }
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) {
// write code here
int a [2000];
int i = 0;
while (pHead1) {
a[i++] = pHead1->val;
pHead1 = pHead1->next;
}
while (pHead2) {
a[i++] = pHead2->val;
pHead2 = pHead2->next;
}
for (int c = i-1; c > 0; c--) {
for (int b = 0; b +1<= c; b++) {
if (a[b] > a[b + 1]) {
int tmp = a[b + 1];
a[b + 1] = a[b];
a[b] = tmp;
}
}
}
struct ListNode* tm = ( struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* tmp = ( struct ListNode*)malloc(sizeof(struct ListNode));
tm->next = tmp;
tmp->next = NULL;
for (int k = 0; k < i; k++) {
struct ListNode* tmp1 = ( struct ListNode*)malloc(sizeof(struct ListNode));
tmp1->val = a[k];
tmp->next = tmp1;
tmp = tmp->next;
}
return tm->next->next;
}