From 77d72f840160095a28d204deda81cc26eda645f7 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 21 Nov 2022 16:37:23 +0100 Subject: [PATCH] c: ex1: add solution --- c/ex1.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 c/ex1.c diff --git a/c/ex1.c b/c/ex1.c new file mode 100644 index 0000000..c4ed9b5 --- /dev/null +++ b/c/ex1.c @@ -0,0 +1,26 @@ +#include +#include + +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; +}