Skip to content

Instantly share code, notes, and snippets.

@wangbj
Created July 9, 2015 01:24
Show Gist options
  • Save wangbj/cd6d972409b327444e3d to your computer and use it in GitHub Desktop.
Save wangbj/cd6d972409b327444e3d to your computer and use it in GitHub Desktop.
After learnt some haskell..
struct ListNode* uncons (struct ListNode* xxs, struct ListNode** xs)
{
struct ListNode* x = xxs;
if (xxs) {
*xs = xxs -> next;
x -> next = NULL;
}
return x;
}
struct ListNode* cons (struct ListNode* x, struct ListNode* xs)
{
x -> next = xs;
return x;
}
struct ListNode* concat (struct ListNode* xs, struct ListNode* ys)
{
if (!xs) return ys;
struct ListNode* p = xs, *q = p->next;
while(q) {
p = q;
q = p->next;
}
p->next = ys;
return xs;
}
struct ListNode* singleton(struct ListNode* x)
{
if (x) x -> next = NULL;
}
struct ListNode* reverse(struct ListNode* xxs)
{
if (!xxs) return NULL;
struct ListNode* x, *xs;
x = uncons (xxs, &xs);
return concat (reverse(xs), singleton(x));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment