/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2) {
ListNode* temp1 = pHead1;
ListNode* temp2 = pHead2;
ListNode* head = new ListNode(0);
ListNode* tail = head;
while (temp1 != NULL && temp2 != NULL)
{
if (temp1->val <= temp2->val)
{
tail->next = temp1;
tail = tail->next;
temp1 = temp1->next;
}
else
{
tail->next = temp2;
tail = tail->next;
temp2 = temp2->next;
}
}
if (temp1 != NULL)
{
tail->next = temp1;
}
else if (temp2 != NULL)
{
tail->next = temp2;
}
tail = head->next;
delete head;
return tail;
}
};
合并两个排序的链表
最新推荐文章于 2024-11-14 22:19:56 发布