# (Easy) 21. Merge Two Sorted Lists ## Run Code  ## 題意 合併兩個List ## 解題思路 就合併兩個List啊 ## 困難之處 我懷念寫cpp檔,搞懂他的function花了我好多時間,我好爛== ## Code ```cpp= /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} i have no idea * ListNode(int x) : val(x), next(nullptr) {} i have no idea * ListNode(int x, ListNode *next) : val(x), next(next) {} i have no idea * }; */ class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1==NULL) return l2; if(l2==NULL) return l1; if(l2->val <= l1->val){ l2->next = mergeTwoLists(l1, l2->next); return l2; } else{ l1->next = mergeTwoLists(l1->next, l2); return l1; } } }; ``` ###### tags: `leetcode`
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up