Skip to content

Instantly share code, notes, and snippets.

@Isekai-Seikatsu
Last active Dec 10, 2019
Embed
What would you like to do?
It's the homework of my college, implementing a function to reverse a simple one-way linked list. And I think this function I came up with was fun.
/*
.
.
.
typedef struct reg {
int ID;
int score;
struct reg *next;
} tReg;
.
.
.
*/
void *_reverse_after(tReg *node) {
void **p;
if (node->next == NULL) {
p = malloc(sizeof(void *) * 2);
p[0] = node; // tail
} else {
p = _reverse_after(node->next);
*(tReg **)p[1] = node;
}
p[1] = &node->next;
return p;
}
void reverse(tReg **head) {
void **p = _reverse_after(*head);
*head = p[0];
*(tReg **)p[1] = NULL;
free(p);
}
/*
.
.
.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment