typedef struct ListNode{ int val; struct ListNode *next; }ListNode; /* 迭代 */ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ if(l1 == NULL) return l2; if(l2 == NULL) return l1; struct ListNode* p1 = l1; struct ListNode* p2 = l2; struct ListNode* l3,*p3; if(l1->val < l2->val) { l3 = l1; l1 = l1->next; p3 = l3; } else { l3 = l2; l2 = l2->next; p3 = l2; } while(p1 && p2) { if(p1->val <= p2->val) { p3->next = p1; p3 = p1; p1 = p1->next; } else { p3->next = p2; p3 = p2; p2 = p2->next; } } p3->next = p1?p1:p2; free(l2); return l3; } /* 递归 */ struct ListNode* mergeTwoLists2(struct ListNode* l1, struct ListNode* l2) { if(l1 == NULL) return l2; if(l2 == NULL) return l1; if(l1->val <= l2->val) { l1->next = mergeTwoLists(l1->next,l2); return l1; } else { l2->next = mergeTwoLists(l1,l2->next); return l2; } }