LeetCode NO.21 cpp(4.1)

标签:链表

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
vector<int> A;
ListNode *tempL1 = l1;
while (tempL1 != nullptr) {
	A.push_back(tempL1->val);
	tempL1 = tempL1->next;
}
ListNode *tempL2 = l2;
while (tempL2 != nullptr) {
	A.push_back(tempL2->val);
	tempL2 = tempL2->next;
}
sort(A.begin(), A.end(), less<int>());

ListNode *l3 = new ListNode(1);
ListNode *ptr = l3;
for (auto iter = A.begin(); iter != A.end(); ++iter)
{
	ListNode *temp = new ListNode(*iter);
	ptr->next = temp;
	ptr = ptr->next;
}
return l3->next;
    }
};

在这里插入图片描述

发布了14 篇原创文章 · 获赞 0 · 访问量 149

猜你喜欢

转载自blog.csdn.net/weixin_45438011/article/details/104902093