#include <iostream>

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

Node *createList()
{
  Node *head = new Node;
  head->data = 0;
  head->next = NULL;
  return head;
}

void showList(Node *head)
{
  while (head->next != NULL)
  {    
    head = head->next;    
    std::cout << head->data << ' ';    
  }
}

void insertNode(Node *head, int num)
{
  Node *p = new Node;
  p->data = num;
  Node *q = head;
  while(q->next != NULL)
    q = q->next;
  q->next = p;
  p->next = NULL;
}

Node *reverseList(Node *head)
{
  Node *p, *q, *r1, *r2; //辅助指针
  r1 = head; //带头结点的链表,保存头结点
  r2 = head->next;
  p = r1->next;
  q = p->next;

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

  p->next = head;
  q->next = p;
  r1->next = q;
  r2->next = NULL;

  return r1;
}

void main()
{
  Node *head1 = createList();
  Node *head2;

  insertNode(head1, 1);
  insertNode(head1, 2);
  insertNode(head1, 3);
  insertNode(head1, 4);
  insertNode(head1, 5);
  insertNode(head1, 6);
  insertNode(head1, 7);
        
        showList(head1);

  head2 = reverseList(head1);
  std::cout << std::endl;

        showList(head2);    
}