50141. Merge Link Lists

難度:4.4/5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <stdlib.h>
#include "merge.h"

// typedef struct node {
// int data;
// struct node *next;
// }Node;

Node *merge(Node *list[], int k){
Node* head = malloc(sizeof(Node));
Node* prev = head;

int cnt = 0;
for(int i = 0; i < k; i++) if(list[i] != NULL) cnt++;
while(cnt > 0){
for(int i = 0; i < k; i++){
if(list[i] == NULL) continue;
Node* temp = list[i];
list[i] = list[i]->next;
if(list[i] == NULL) cnt--;
prev->next = temp;
prev = prev->next;
}

for(int i = k - 1; i >= 0; i--){
if(list[i] == NULL) continue;
Node* temp = list[i];
list[i] = list[i]->next;
if(list[i] == NULL) cnt--;
prev->next = temp;
prev = prev->next;
}
}
return head->next;
}

50141. Merge Link Lists
https://aaronlin1229.github.io/judgegirl_50141/
Author
Akizumi
Posted on
July 17, 2023
Licensed under