/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct stack
{
struct ListNode* code;
struct stack* next;
};
void push(struct stack** top,struct ListNode* newcode)
{
struct stack* temp=(struct stack*)malloc(sizeof(struct stack));
temp->next=*top;
temp->code=newcode;
*top=temp;
}
struct ListNode* pop(struct stack** top)
{
struct stack* temp;
struct ListNode* newcode;
if(*top)
{
temp=*top;
newcode=temp->code;
*top=temp->next;
free(temp);
return newcode;
}
else
return NULL;
}
struct ListNode* reverseList(struct ListNode* head){
struct stack* top=NULL;
struct ListNode* temp;
if(!head)
return head;
if(!head->next)
return head;
while(head->next)
{
push(&top,head);
head=head->next;
}
temp=head;
do
{
head->next=pop(&top);
head=head->next;
}while(head);
head=temp;
return head;
}