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

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

node *push(node *head, int k);
node *list_invert(node *head);
void print_list(node *head);
void listfree(node *head);

//Insert from head like stack.
node* push(node *head, int k){
    node *new = malloc(sizeof(node));
    new -> data = k;
    new -> next = NULL;     
    if(head == NULL){
        return new;
    }
    else{
        new -> next = head; //We have to set the pointer of inserted node
        head = new;
        return head;
    }

}

node *list_invert(node *head){
    node *curr = head, *prev = NULL, *succ = curr -> next;   
    while(succ != NULL){    //最後一個node不會做下列操作
        curr -> next = prev;
        prev = curr;
        curr = succ; 
        succ = succ -> next;
    }
    curr -> next = prev; // 把最後一個node的pointer反過來。
    head = curr;
    return head; 
}

void print_list(node* head){
    while(head != NULL){
        printf("%d -> ", head -> data);
        head = head -> next;
    }
    printf("NULL\n");
}



int main(int argc, char **argv){
    node *head = NULL; //Empty Linked List;
    head = push(head, 2);
    head = push(head, 7);
    head = push(head, 15);
    head = push(head, 21);

    print_list(head);
    head = list_invert(head);
    print_list(head);


    return 0;
}
