#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct node {
	int data;
	struct node *next;
};

void swap(struct node *a, struct node *b) {
	struct node tmp;
	memcpy(&tmp, a, sizeof(tmp));
	memcpy(a, b, sizeof(tmp));
	memcpy(b, &tmp, sizeof(tmp));
}

void reverse(struct node *h) {
	if (!h || !h->next) {
		return;
	}
	struct node *tail = h->next;
	while (tail->next) {
		tail = tail->next;
	}
	swap(h, tail);
	struct node *p = NULL;
	struct node *c = tail;
	do {
		struct node *n = c->next;
		c->next = p;
		p = c;
		c = n;
	} while (c->next != tail);
	h->next = c;
}

void print(struct node *p) {
	while (p) {
		printf("%d ", p->data);
		p = p->next;
	}
	printf("\n");
}

int main(void) {
	struct node *head = malloc(sizeof(struct node));
	head->data = 1;
	head->next = malloc(sizeof(struct node));
	head->next->data = 2;
	head->next->next = malloc(sizeof(struct node));
	head->next->next->data = 3;
	head->next->next->next = NULL;
	print(head);
	reverse(head);
	printf("------\n");
	print(head);
	return 0;
}
