题解 | #合并两个排序的链表#
合并两个排序的链表
https://www.nowcoder.com/practice/d8b6b4358f774294a89de2a6ac4d9337
/**
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2) {
// write code here
struct ListNode* t = NULL; struct ListNode H; struct ListNode* h1 = &H;
h1->next = pHead1; struct ListNode* temp = pHead2;
if (pHead1 == NULL) return pHead2;
if (pHead2 == NULL) return pHead1;
while (h1->next != NULL){
if (temp->val < h1->next->val){
t = temp; temp = h1->next;
h1->next = t; h1 = h1->next;
}
else h1 = h1->next;
}
h1->next = temp;
return H.next;
}
#数据结构编程链表#
*
* @param pHead1 ListNode类
* @param pHead2 ListNode类
* @return ListNode类
*/
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2) {
// write code here
struct ListNode* t = NULL; struct ListNode H; struct ListNode* h1 = &H;
h1->next = pHead1; struct ListNode* temp = pHead2;
if (pHead1 == NULL) return pHead2;
if (pHead2 == NULL) return pHead1;
while (h1->next != NULL){
if (temp->val < h1->next->val){
t = temp; temp = h1->next;
h1->next = t; h1 = h1->next;
}
else h1 = h1->next;
}
h1->next = temp;
return H.next;
}
#数据结构编程链表#