题目:
归并:
#include "iostream"using namespace std;struct ListNode {int value;ListNode *next;ListNode(int x) : value(x), next(NULL) {}};class Solution {public:ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {if (l1 == nullptr)return l2;if (l2 == nullptr)return l1;if (l1->value < l2->value) {l1->next = mergeTwoLists(l1->next, l2);return l1;} else {l2->next = mergeTwoLists(l1, l2->next);return l2;}}};int main() {ListNode l1(1);ListNode l2(5);ListNode l3(9);l1.next = &l2;l2.next = &l3;ListNode b1(3);ListNode b2(4);b1.next = &b2;Solution s;s.mergeTwoLists(&l1, &b1);for (int i = 0; i < 5; ++i) {cout << l1.value << endl;l1 = *l1.next;}}//class Solution {//public:// ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {//// }//};
