合并 k 个升序的链表并将结果作为一个升序的链表返回其头节点。
数据范围:节点总数
,每个节点的val满足
要求:时间复杂度
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类一维数组
* @param listsLen int lists数组长度
* @return ListNode类
*/
struct ListNode* findNode(struct ListNode **lists, int listsLen);
struct ListNode* findNode(struct ListNode **lists, int listsLen)
{
struct ListNode *minNode = NULL;
int i, minIndex = -1;
for (i = 0; i < listsLen; i++) {
if (lists[i]) {
if (!minNode ) {
minNode = lists[i];
minIndex = i;
break;
}
}
}
if(minIndex == -1)
return NULL;
//寻找最小值节点
for (i = minIndex + 1; i < listsLen; i++) {
if (lists[i] != NULL && lists[i]->val < minNode->val) {
minNode = lists[i];
minIndex = i;
}
}
// 移动对应链表的头指针
lists[minIndex] = lists[minIndex]->next;
return minNode;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
// write code here
struct ListNode prev, *head, *tmp;
prev.next = NULL;
tmp = &prev;
while(1)
{
tmp->next = findNode(lists, listsLen);
if(!tmp->next)
break;
tmp = tmp->next;
}
return prev.next;
}
struct ListNode * get_next_min_node(struct ListNode **pNextTbl, int listsLen, int *list_left) {
int min = -1;
int cnt = listsLen;
struct ListNode *pNode = NULL;
for (int i = 0; i < listsLen; i++) {
if (!pNextTbl[i]) {
cnt--;
continue;
}
if (min == -1) {
min = i;
} else {
min = (pNextTbl[i]->val > pNextTbl[min]->val) ? min : i;
}
}
if (min == -1) {
return NULL;
}
*list_left = cnt;
pNode = pNextTbl[min];
pNextTbl[min] = pNextTbl[min]->next;
return pNode;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen) {
// write code here
struct ListNode **pNextTbl = NULL;
struct ListNode *pList = NULL;
struct ListNode *pTmp= NULL;
struct ListNode *pListHead = NULL;
int list_left = 0;
if (!lists) {
return NULL;
}
pNextTbl = malloc(sizeof(struct ListNode *)*listsLen);
if (!pNextTbl) {
return NULL;
}
memset(pNextTbl, 0, sizeof(struct ListNode *)*listsLen);
for (int i = 0; i < listsLen; i++) {
if (lists[i]) {
list_left++;
pNextTbl[i] = lists[i];
}
}
while ((pTmp = get_next_min_node(pNextTbl, listsLen, &list_left)) != NULL) {
if (pList) {
pList->next = pTmp;
} else {
pListHead = pTmp;
}
pList = pTmp;
};
free(pNextTbl);
return pListHead;
} /**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类一维数组
* @param listsLen int lists数组长度
* @return ListNode类
*/
struct ListNode* merge(struct ListNode* p1, struct ListNode* p2)
{
//合并函数,合并两链表
struct ListNode* mhead = (struct ListNode*)malloc(sizeof(struct ListNode));
mhead->next = p1;
struct ListNode* cur = mhead;
while(p1 && p2)
{
if(p1->val <= p2->val)
{
cur->next = p1;
p1 = p1->next;
}
else
{
cur->next = p2;
p2 = p2->next;
}
cur = cur->next;
}
if(p1)
{
cur->next = p1;
}
if(p2)
{
cur->next = p2;
}
return mhead->next;
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
// write code here
struct ListNode* mhead = NULL;
while(listsLen--)
{
mhead = merge(mhead, lists[listsLen]);
}
return mhead;
} 在上题基础上加个循环累加就可。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类一维数组
* @param listsLen int lists数组长度
* @return ListNode类
*/
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ){
struct ListNode* pHead =(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode* beginHead = NULL;
int min=1001, minnum=5001;
int endflag = 0;
for(int i=0; i<listsLen; i++)
{
if(lists[i] == NULL)
continue;
else if(lists[i] -> val < min)
{
min = lists[i] -> val;
minnum = i;
}
}
if(minnum == 5001) return NULL;
beginHead = lists[minnum];
pHead = lists[minnum];
lists[minnum] = lists[minnum] -> next;
min = 1001;
minnum = 5001;
while(endflag == 0)
{
//当链表为空的时候再取链表指针中的值的时候会报错,所以要提前把边界取出来
for(int i=0; i<listsLen; i++)
{
if(lists[i] == NULL)
continue;
else if(lists[i] -> val < min)
{
min = lists[i] -> val;
minnum = i;
}
}
if(min == 1001 && minnum ==5001)
endflag = 1;
else
{
pHead -> next = lists[minnum];
pHead = lists[minnum];
lists[minnum] = lists[minnum] -> next;
min = 1001;
minnum = 5001;
}
// write code here
}
return beginHead;
}
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类一维数组
* @param listsLen int lists数组长度
* @return ListNode类
*/
int lenth(struct ListNode* p1, struct ListNode* p2){
int len = 0;
while(p1 != NULL){
len++;
p1 = p1->next;
}
while(p2 != NULL){
len++;
p2 = p2->next;
}
return len;
}
void mergeSort(struct ListNode* p1, struct ListNode* p2, int len){
int* B = (int*)malloc(sizeof(int)*len), k = 0;
struct ListNode* p3, *p4;
p3 = p1;
p4 = p2;
while(p3 != NULL && p4 != NULL){
if(p3->val <= p4->val){
B[k++] = p3->val;
p3 = p3->next;
}else{
B[k++] = p4->val;
p4 = p4->next;
}
}
while(p3 != NULL){
B[k++] = p3->val;
p3 = p3->next;
}
while(p4 != NULL){
B[k++] = p4->val;
p4 = p4->next;
}
p3 = p1;
while(p3->next != NULL){
p3 = p3->next;
}
p3->next = p2;
for(p3 = p1,k =0;k < len;k++){
p3->val = B[k];
p3 = p3->next;
}
free(B);
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
// write code here
struct ListNode* p1 = NULL, *p2 = NULL;
int i = 0,j,Lenth;
while(lists[i] == NULL && i < listsLen){
i++;
}
p1 = lists[i];
if(i != listsLen - 1){
for(j = i + 1;j < listsLen;j++){
p2 = lists[j];
Lenth = lenth(p1,p2);
mergeSort(p1, p2, Lenth);
}
return p1;
}else{
return p1;
}
} void AddListNode(struct ListNode** head, int m, int val) {
struct ListNode *NewNode,*p1;
NewNode = (struct ListNode *)malloc(sizeof(struct ListNode));
NewNode->val = val;
if(!m) {
NewNode->next = *head;
*head = NewNode;
return;
}
p1 = *head;
while((--m>0)&&(p1->next!=NULL))
p1 = p1->next;
{
struct ListNode *buffer;
buffer = p1->next;
p1->next = NewNode;
NewNode->next = buffer;
}
}
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
struct ListNode *res=NULL,*p1,*p2;
if(!listsLen)
return NULL;
res = mergeKLists(lists, listsLen-1);
p1 = lists[listsLen-1];
if(p1 == NULL)
return res;
if(res == NULL)
return p1;
do {
int AddLoc = 0;
p2 = res;
do {
if(p1->val <= p2->val) {
AddListNode(&res, AddLoc, p1->val);
break;
}
AddLoc++;
p2 = p2->next;
}while(p2 != NULL);
if(p2 == NULL)
AddListNode(&res, AddLoc, p1->val);
p1 = p1->next;
}while(p1 != NULL);
return res;
} 这是我第一次完全自己调通的稍微复杂的递归,总结一句:特殊情况必须考虑,不然递归不通。我没有看解题思路,肯定没有官方的好,不算是最好的代码,但已经成就满满。高手解题思路才是真正的简。
#include <stdlib.h>
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
// write code here
struct ListNode* head = NULL;
struct ListNode* p = *lists;
struct ListNode* t = (struct ListNode*)malloc(sizeof(struct ListNode)); //归并后序列
head = t;
for(int i = 1; i < listsLen; i++)
{
struct ListNode* q = lists[i];
while (p != NULL || q != NULL)
{
if(p == NULL)
{
t->next = q;
break;
}
if(q == NULL)
{
t->next = p;
break;
}
if(p->val < q->val)
{
t->next = p;
p = p->next;
t = t->next;
}
else
{
t->next = q;
q = q->next;
t = t->next;
}
}
p = head->next;
t = head;
}
head = head->next;
return head;
} #include <stdlib.h>
int s[5000];
int privot(int a[], int low, int high);
void quicksort(int a[], int low, int high);
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
// write code here
int i = 0;
int j = 0;
struct ListNode *p;
//将所有链表的值赋给数组s
for (j = 0; j < listsLen; j++){
p = lists[j];
while(p) {
s[i++] = p->val;
p = p->next;
}
}
//对数组s进行快排
quicksort(s, 0, i-1);
struct ListNode *q = (struct ListNode *)malloc(sizeof(struct ListNode));
q->next = NULL;
p = q;
//将排好序的值一一赋给新的链表
for (j = 0; j < i; j++){
struct ListNode *r = (struct ListNode *)malloc(sizeof(struct ListNode));
r->val = s[j];
r->next = NULL;
p->next = r;
p = p->next;
}
return q->next;
}
//划分
int privot(int a[], int low, int high) {
int temp = a[low];
while (low < high){
while (low < high && a[high] >= temp) high--;
a[low] = a[high];
while (low < high && a[low] <= temp) low++;
a[high] = a[low];
}
a[low] = temp;
return low;
}
//快排
void quicksort(int a[], int low, int high){
if (low < high) {
int pri = privot(a, low, high);
quicksort(a, low, pri-1);
quicksort(a, pri+1, high);
}
} /**
* 合并两个链表
*/
struct ListNode *mergeList(struct ListNode *left, struct ListNode *right)
{
struct ListNode *head = malloc(sizeof(struct ListNode));
struct ListNode *node = head;
while (left && right)
{
if (left->val < right->val)
{
node->next = left;
left = left->next;
}
else
{
node->next = right;
right = right->next;
}
node = node->next;
}
node->next = left ? left : right;
node = head->next;
free(head);
return node;
}
/**
* @description 头尾链表合并到一起,更新头链表,直到只剩下一个链表
* @param lists ListNode类一维数组
* @param listsLen int lists数组长度
* @return ListNode类
*/
struct ListNode *mergeKLists(struct ListNode **lists, int listsLen)
{
// write code here
if (listsLen <= 0) {
return NULL;
}
int st = 0, ed = listsLen - 1;
while (ed > 0)
{
st = 0;
while (st < ed && ed)
{
lists[st] = mergeList(lists[st], lists[ed]);
st++;
ed--;
}
}
return lists[0];
}
/**
*
* @param lists ListNode类一维数组
* @param listsLen int lists数组长度
* @return ListNode类
*/
struct ListNode* mergeKLists(struct ListNode** lists, int listsLen ) {
// write code here
struct ListNode* head = NULL;
int i = 0;
for(i = 0; i < listsLen; i++)
{
struct ListNode* ptr1 = lists[i];
if(ptr1 == NULL)
continue;
if(head == NULL)
{
head = ptr1;
continue;
}
struct ListNode* ptr2 = head, *ptr3 = NULL;
while(ptr1 != NULL && ptr2 != NULL)
{
if(ptr1->val >= ptr2->val && ptr2->next != NULL && ptr1->val <= ptr2->next->val){
ptr3 = ptr1->next;
ptr1->next = ptr2->next;
ptr2->next = ptr1;
ptr1 = ptr3;
ptr2 = ptr2->next;
}else if(ptr1->val < ptr2->val){
head = ptr1;
ptr3 = ptr1->next;
ptr1->next = ptr2;
ptr2 = ptr1;
ptr1 = ptr3;
}else if(ptr2->next == NULL){
ptr2->next = ptr1;
break;
}else{
ptr2 = ptr2->next;
}
}
if(ptr1 != NULL && ptr2 == NULL)
{
ptr2->next = ptr1;
}
}
return head;
}