c: ex1: add solution

This commit is contained in:
Bruno BELANYI 2022-11-21 16:37:23 +01:00
parent 935d9df675
commit 77d72f8401
1 changed files with 26 additions and 0 deletions

26
c/ex1.c Normal file
View File

@ -0,0 +1,26 @@
#include <stdbool.h>
#include <stddef.h>
struct node_t {
unsigned v;
struct node_t* next;
};
struct node_t* even_nodes(struct node_t** list) {
struct node_t* res = NULL;
while (true) {
struct node_t* next = (*list)->next;
if ((*list)->v % 2 == 0) {
// Pop element from the list, add it to head of the other
(*list)->next = res;
res = *list;
}
*list = next;
}
// NOTE: this is in reverse order from the input list
return res;
}