class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
{
ListNode tempHead(0);//设置合并的新链表临时头节点
ListNode* pre=&tempHead;//设置一个用来遍历表示当前位置的指针
while(pHead1&&pHead2){
if(pHead1->valval)//比较两个链表节点的大小,较小的一个先放入新的链表中
{pre->next=pHead1;
pHead1=pHead1->next;}
else
{pre->next=pHead2;
pHead2=pHead2->next;}
pre=pre->next;
}
if(!pHead1&&pHead2){//若两个链表中其中一个还有剩余,把剩余的部分直接补充到新链表的末尾
pre->next=pHead2;
}
if(!pHead2&&pHead1){
pre->next=pHead1;
}
return tempHead.next;
}
};