class ListNode {
friend class MergeListNode;
public:
ListNode()=default;
ListNode(int val) :data(val), next(nullptr) {}
void insertNode(ListNode * node, int pos);
void deleteNode(int pos);
ListNode * initialList();
int getsize();
void insertBack(ListNode *node);
void showList();
void ListNodeSort();
void ListNodeTopK();
private:
ListNode *next;
int data;
};
int ListNode::getsize() {
int count = 0;
ListNode *tar = this;
while (tar->next) { tar = tar->next; count++; }
return count;}
void ListNode::insertNode(ListNode *node, int pos) {
if (pos > (this->getsize())) { throw std::out_of_range("out of range"); }
int count = 0;
ListNode *tarl = this;
while (count<pos) {
tarl = tarl->next;
count++;
}
ListNode * temp = tarl->next->next;;
tarl->next = node;
node->next = temp;
}
void ListNode::deleteNode(int pos) {
if (pos > this->getsize()) { throw std::out_of_range("out of range"); }
int count = 0;
ListNode * tarl = this;
while (count < pos-1) {
tarl = tarl->next;
count++;
}
ListNode *temp = tarl->next->next;
ListNode *delp = tarl->next;
tarl->next = temp;
delete delp;
}
void ListNode::showList() {
ListNode *temp = this;
while (temp->next) {
std::cout << temp->next->data << "->";
temp = temp->next;
}
}
ListNode * ListNode::initialList() {
bool flage = true;
ListNode * temp = this;
while (flage) {
std::cin >> data;
if (data == -1) { flage = false; return this; }
ListNode *newNode = new ListNode(data);
temp->next = newNode;
temp = newNode;
}
return this;
}
void ListNode::insertBack(ListNode *node) {
ListNode *tar = this;
while (tar->next != nullptr) {
tar = tar->next;
}
tar->next = node;
}
void ListNode::ListNodeSort() {
std::vector<ListNode *> vec;
ListNode * tarl = this;
while (tarl->next) {
tarl = tarl->next;
vec.push_back(tarl);
}
auto comp = [](ListNode *node1, ListNode *node2) {return (node1->data) > (node2->data); };
std::sort(vec.begin(), vec.end(), comp);
ListNode *tar = this;
while (!vec.empty()) {
auto temp = vec.back();
tar->next = temp;
vec.pop_back();
tar = temp;
}
tar->next = nullptr;
}
void ListNode::ListNodeTopK() {
auto compare = [](ListNode* node1, ListNode* node2) {return node1->data > node2->data; };
std::priority_queue<ListNode*, std::vector<ListNode*>, decltype(compare)> pro_q(compare);
ListNode *iterator = this;
while (iterator->next) {
iterator = iterator->next;
if (pro_q.size() < 3)
pro_q.push(iterator);else {
pro_q.pop();
pro_q.push(iterator);
}
}
while (!pro_q.empty()) {
std::cout << pro_q.top()->data << std::endl;
pro_q.pop();
}
}
class MergeListNode {
public:
std::vector<int> static MergListNode(std::vector<ListNode*>);
};
std::vector<int> MergeListNode::MergListNode(std::vector<ListNode*> vec) {
std::vector<int> res;
auto compare = [](ListNode *node1, ListNode *node2) { return node1->data > node2->data; };
std::priority_queue<ListNode *, std::vector<ListNode *>, decltype(compare)> pro_q(compare);
for (auto ite = vec.begin(); ite != vec.end(); ite++) {
if ((*ite)->next == nullptr) { std::cout << "合并的链表有一个为空" << std::endl; break; }
else pro_q.push((*ite)->next);
}
while (!pro_q.empty()) {
if (pro_q.size() < vec.size()){
if (pro_q.top()->next) { pro_q.push(pro_q.top()->next); res.push_back(pro_q.top()->data); pro_q.pop(); }
else { res.push_back(pro_q.top()->data); pro_q.pop(); }
}
else {
auto temp = pro_q.top();
res.push_back(temp->data);
pro_q.pop();
if (temp->next) { pro_q.push(temp->next);
}
}
}
return res;}