c: ex1: add solution
This commit is contained in:
parent
935d9df675
commit
77d72f8401
26
c/ex1.c
Normal file
26
c/ex1.c
Normal 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;
|
||||||
|
}
|
Loading…
Reference in a new issue