善用stable_sort
然后开一个新的vector去重
别忘记改指针
#include#include #include using namespace std; // typedef struct node { // int data; // struct node* next; // // 其他数据域成员 // }NODE; bool cmp(NODE* A, NODE* B) { return A->data < B->data; } NODE* distinctSort(NODE* head) { if(!head) return NULL; vector v; NODE* h = head; while(h != NULL) { v.push_back(h); h = h->next; } stable_sort(v.begin(), v.end(), cmp); vector out; for(int i = 0; i < v.size(); i++) { if(!i) out.push_back(v[i]); else if(v[i]->data != v[i - 1]->data) { out.push_back(v[i]); out[out.size()-2]->next = out[out.size()-1]; } } out[out.size()-1]->next = NULL; return out[0]; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)