Victus Spiritus

home

Tiny brain teaser C code puzzle

09 Apr 2011

Today's post is a simple morning mystery function. What am I?
(beware spoilers in the comments)

typedef struct node {
  /* data struct */
  struct node *next;
} node;

node* mystery(node* first) {
  node * prev=NULL;
  node * temp=NULL;
  node * cur=first;

  while (cur != NULL) {
    temp = cur->next;
    cur->next = prev;
    prev = cur;
    cur = temp;
  }
  return prev;
}

Thanks for error catching from redditors.

*update*: GoldyOrNugget has a functional programming python example which you can work through.

def magic_function(*fs):
  return lambda x: reduce(lambda a,b: b(a), (fs+x)[::-1])