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

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

struct node *head = NULL;

void appendNode(int data)
{
	struct node *newnode = (struct node *)malloc(sizeof(struct node));
	newnode->data = data;
	newnode->next = NULL;

	if(head == NULL)
		head = newnode;
	else
	{
		struct node *temp = head;
		while(temp->next)
			temp = temp->next;
		temp->next = newnode;
	}
}

void displayNodes()
{
	struct node *temp = head;

	printf("HEAD->");
	if(temp)
        {
		while(temp)
                {
			printf("%d->",temp->data);
                        temp = temp->next;
                }
        }
	printf("NULL");
}

//REVERSING THE LINKEDLIST BY RECURSION
void reverseListByRecursion(struct node *currNode)
{
	if(currNode==NULL)
		return;
	else
	{
		reverseListByRecursion(currNode->next);
		printf("%d->",currNode->data);
	}
}

//REVERSING THE LINKEDLIST BY USING POINTERS
void reverseListByPointers()
{
	struct node *p = head,
		    *q = NULL,
		    *r;

	while(p!=NULL)
	{
		r = q;
		q = p;
		p = p->next;
		q->next = r;
	}

	head = q;
}


int main()
{
	appendNode(123);
	appendNode(234);
	appendNode(345);

	displayNodes();

    //REVERSING THE LINKEDLIST BY RECURSION
    printf("\n\nHEAD->");
	reverseListByRecursion(head);
	printf("NULL");

    //REVERSING THE LINKEDLIST BY USING POINTERS
    
	reverseListByPointers();
	printf("\n\nReversed LL by pointers");
	displayNodes();
	
        return 0;
}