合并有序链表(声明一个头指针)
合并有序链表
http://www.nowcoder.com/questionTerminal/a479a3f0c4554867b35356e0d57cf03d
/** 添加一个赋值头结点p。 */ class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode *p = new ListNode(0), *p1 = l1, *p2 = l2, *pA = p; while(p1 && p2){ if(p1->val < p2->val) {p->next = p1; p = p1; p1 = p1->next;} else {p->next = p2; p = p2; p2 = p2->next;} } if(p1){ p->next = p1; } if(p2){ p->next = p2; } return pA->next; } };