From e0b77729a1508c812bccfd879f8045cb8a195331 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 9 May 2022 19:33:30 +0800 Subject: [PATCH] =?UTF-8?q?36.=20=E5=90=88=E5=B9=B6=E4=B8=A4=E4=B8=AA?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=E7=9A=84=E9=93=BE=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.acwing.com/problem/content/submission/code_detail/14064359/ --- AcWing/36/36.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 AcWing/36/36.cpp diff --git a/AcWing/36/36.cpp b/AcWing/36/36.cpp new file mode 100644 index 00000000..3349aceb --- /dev/null +++ b/AcWing/36/36.cpp @@ -0,0 +1,24 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { + public: + ListNode* merge(ListNode* l1, ListNode* l2) { + if (l1 == nullptr) { + return l2; + } else if (l2 == nullptr) { + return l1; + } else if (l1->val < l2->val) { + l1->next = merge(l1->next, l2); + return l1; + } else { + l2->next = merge(l1, l2->next); + return l2; + } + } +};