#include "lists.h"

struct list* concatenate_all(int count, struct list* lists[]) {
	struct list* first = 0;

	for (int i = 0; i < count; i++) {
		struct list* l = lists[i];
		if (l == 0) continue;
		if (first == 0) {
			first = l;
		}
		while (l->next != 0) {
			l = l->next;
		}
		i++;
		for (; lists[i] == 0 && i < count; i++);
		if (i == count) {
			break;
		}
		l->next = lists[i];
		i--;
	}

	return first;
}

struct list* merge_sorted(struct list* a, struct list* b) {
	if (a == 0) {
		return b;
	} else if (b == 0) {
		return a;
	} else {
		if (a->value < b->value) {
			a->next = merge_sorted(a->next, b);
			return a;
		} else {
			b->next = merge_sorted(a, b->next);
			return b;
		}
	}
}

