/** * 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* p1 = pHead1;//用于遍历链表1 struct ListNode* p2 = pHead2;//用于遍历链表2 struct ListNode* pHead3 = (struct ListNode*)malloc(sizeof(struct ListNode));//暂时的头结点 pHead3->next = NULL; struct ListNode* p3 = pHead3;//用于遍历链表3 while(p1 != NULL && p2 != NULL){ if(p1->val <= p2->val){ p3->next = p1; p3 = p1; p1 = p1->next; } else{ p3->next = p2; p3 = p2; p2 = p2->next; } } if(p1){ p3->next = p1; } else{ p3->next = p2; } return pHead3->next;//这里是暂时头结点的用处 }