#include <stdio.h>
#include <stdlib.h>

struct list {
    int data;
    struct list *next;
};

void prtlst(struct list *ptr) {
    while (ptr) {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("\n");
}

struct list *arr2lst(const int a[], int n) {
    struct list *tmp, *first = NULL;
    for (int i = n-1; i >= 0; i--) {
        tmp = malloc(sizeof(*tmp));
        tmp->data = a[i];
        tmp->next = first;
        first = tmp;
    }
    return first;
}

struct list *arr2lst2(const int a[], int n) {
    struct list *first, *last, *tmp;

    first = NULL;
    for (int i = 0; i < n; i++) {
        tmp = malloc(sizeof(*tmp));
        tmp->data = a[i];
        tmp->next = NULL;
        if (!first)
            first = tmp;
        else
            last->next = tmp;
        last = tmp;
    }
    return first;
}

struct list* arr2lst3(const int a[], int n) {
    struct list *tmp;
    if (!n)
        return NULL;
    tmp = malloc(sizeof(*tmp));
    tmp->data = *a;
    tmp->next = arr2lst3(a + 1, n - 1);
    return tmp;
}

long sumlst(const struct list* l) {
    long sum = 0;
    while (l) {
        sum += l->data;
        l = l->next;
    }
    return sum;
}

void delneglst(struct list **pl) {
    while (*pl) {
        if ((*pl)->data < 0) {
            struct list* tmp = *pl;
            *pl = (*pl)->next;
            free(tmp);
        }
        else
            pl = &(*pl)->next;
    }
}


void delneglst2(struct list **pl) {
    if (!*pl)
        return;
    if ((*pl)->data < 0) {
        struct list* tmp = *pl;
        *pl = (*pl)->next;
        free(tmp);
    }
    delneglst2(&(*pl)->next);
}

void delneglst3(struct list *l) {
    struct list *tmp, *tmp2;
    while (l) {
        if (l->data < 0) {
            tmp = l;
            l = l->next;
            free(tmp);
        } else {
            break;
        }
    }
    if (l) {
        while (tmp = l->next) {
            if (tmp->data < 0) {
                tmp2 = tmp;
                tmp = tmp->next;
                free(tmp2);
                l->next = tmp;
            } else {
                l = l->next;
            }
        }
    }
}

struct list *delneglst4(struct list *l) {
    struct list *res = l;
    if (l) {
        l->next = delneglst4(l->next);
        if (l->data < 0) {
            res = l->next;
            free(l);
        }
    }
    return res;
}

int main() {
    struct list *l = NULL;
    int a[] = {1, -3, 2, -1, 3, 4};
    int n = sizeof(a)/sizeof(a[0]);
    l = arr2lst3(a, n);
    prtlst(l);
    l = delneglst4(l);
    prtlst(l);
    return 0;
}
