题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
/** * 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 struct ListNode* t = malloc(sizeof(struct ListNode)); t->val = -1001; t->next = pHead1; struct ListNode* l1 = t; struct ListNode* l2 = pHead2; while(l1->next != NULL && l2 != NULL) { if(l2->val >= l1->val && l2->val <= l1->next->val) { printf("l2->val is %d and l1->next->val is %d\n", l2->val, l1->next->val); struct ListNode* l1n = l1->next; printf("l1->next val is %d\n", l1n->val); l1->next = l2; while(l2->next != NULL && l2->next->val <= l1n->val) { l2 = l2->next; } l1 = l2; l2 = l2->next; l1->next = l1n; l1 = l1n; } else { l1 = l1->next; } } if(l2 != NULL) { l1->next = l2; } return t->next; }