**
以下为C++版本
**
已知两个已排序链表头节点指针l1与l2,将这两个链表合并,合并后认为有序的,返回合并后的头节点。
思路:
比较l1和l2指向的节点,将较小的节点插入到pre指针后,并向前移动较小节点对应的指针。
#include <stdio.h>
#include <vector>
#include<algorithm>
#include<typeinfo>
struct ListNode
{
int val;
ListNode* next;
ListNode(int x) : val(x), next(NULL) {
}
};
bool cmp(const ListNode* a, const ListNode* b)
{
return a->val < b->val;
}
class Solution
{
public:
Solution() {
}
~Solution() {
}
//方法一:排序。
ListNode* mergeKLists(std::vector<ListNode*>& lists)
{
std::vector<ListNode*> node_vec;
for (unsigned int i = 0; i< lists.size(); i++)
{
ListNode* head = lists[i];