链表反转

#include <iostream>
using namespace std;
typedef struct ListNode
{
 int value;
 ListNode *next;

}ListNode;
 ListNode *Reverse (ListNode *head)
{
  ListNode *pnext;         
  ListNode *pre;        
  ListNode *pcur;        
 if(head->next==NULL)
  return head;
 pre = NULL;            
 pcur = head->next;             
 pnext = pcur->next;
 while(pnext!= NULL)
 {
  pcur->next = pre;
  pre = pcur;
  pcur = pnext;
  pnext = pcur->next;
 }
 pcur->next = pre;
 head->next = pcur;
 return head;
}

void traverse_link(ListNode *pHead)
{
 if(pHead==NULL)
  return;
 ListNode *p = pHead->next;
 while(p!=NULL)
 {
  printf("%d ",p->value);
  p=p->next;
 }
 printf(" ");
}

void create_link(ListNode *pHead)
{
 ListNode *pre;
 pHead->next = NULL;
 pre = pHead;
 for (int i =0;i<7;i++)
 {
  ListNode *ptemp = new ListNode;
  ptemp->value = i;
  ptemp->next=NULL;
  pre->next = ptemp;
  pre = ptemp;

 }
}

int main()
{
 ListNode *head = new ListNode;;
 create_link(head);
 traverse_link(head);
 Reverse(head);
 traverse_link(head);
 system("pause");
 return 0;
}

原文地址:https://www.cnblogs.com/cheng07045406/p/3286323.html