/**

  • struct ListNode {
  • int val;
  • struct ListNode *next;
  • };
  • C语言声明定义全局变量请加上static,防止重复定义 */

/** *

  • @param pHead1 ListNode类
  • @param pHead2 ListNode类
  • @return ListNode类 / struct ListNode Merge(struct ListNode* pHead1, struct ListNode* pHead2 ) { // write code here //如果其中一个链表为空,返回另一个 if(pHead1==NULL) return pHead2; if(pHead2==NULL) return pHead1; struct ListNode*head=NULL,*tail=NULL; while(pHead1&&pHead2) { if(pHead1->valval) { if(head==NULL) { head=tail=pHead1; } else { tail->next=pHead1; tail=pHead1; } pHead1=pHead1->next; } else { if(head==NULL) { head=tail=pHead2; } else { tail->next=pHead2; tail=pHead2; } pHead2=pHead2->next; } } if(pHead1) { tail->next=pHead1; } if(pHead2) { tail->next=pHead2; } return head; }