<pre name="code" class="cpp">struct ListNode *MergeSortList(struct ListNode *head1, struct ListNode *head2)
{
struct ListNode *p1, *p2, *p, *temp;
p = (struct ListNode *)malloc(sizeof(struct ListNode));
temp = p; //暂存合并后的链表的头结点
p1 = head1;
p2 = head2;
while (p1&&p2)
{
if (p1->val <= p2->val)
{
p->next = p1;
p1 = p1->next;
}
else
{
p->next = p2;
p2 = p2->next;
}
p = p->next;
}
if (p1)
p->next = p1;
else
p->next = p2;
p = temp->next;
return p;
}
LeetCode Merge Two Sorted Lists(合并两个有序链表)
最新推荐文章于 2022-12-07 22:31:38 发布