• 我先画一个单链表,这个单链表有4个元素。我的思路就是,每次把第二个元素提到最前面来。比如下面是第一次交换,我们先让头结点的next域指向结点a2,再让结点a1的next域指向结点a3,最后将结点a2的next域指向结点a1,就完成了第一次交换。
第一次交换
  • 然后进行相同的交换将结点a3移动到结点a2的前面,然后再将结点a4移动到结点a3的前面就完成了反转。
第二次交换
第三次交换
  • 思路有了,那就可以写代码了。这里我们需要额外的两个工作指针来辅助交换。这个下面的步骤慢慢理解下,结合图片。注意结点之间的关系要先断再连。

步骤:

  1. 定义当前结点 current,初始值为首元结点,current = L->next;
  2. 定义当前结点的后继结点 pnext, pnext = current->next; 
  3. 只要 pnext 存在,就执行以下循环:
    • 定义新节点 prev,它是 pnext的后继结点,prev = pnext->next;
    • 把pnext的后继指向current, pnext->next = current;
    • 此时,pnext 实际上已经到了 current 前一位成为新的current,所以这个时候 current 结点实际上成为新的 pnext,current = pnext;
    • 此时,新的 current 就是 pnext,current = pnext;
    • 而新的 pnext 就是 prev,pnext = prev;
  4. 最后将头结点与 current 重新连上即可,L->next = current;

函数设计如下:

/* 单链表反转/逆序 */ Status ListReverse(LinkList L) {     LinkList current,pnext,prev;     if(L == NULL || L->next == NULL)         return L;     current = L->next;  /* p1指向链表头节点的下一个节点 */     pnext = current->next;     current->next = NULL;     while(pnext)     {         prev = pnext->next;         pnext->next = current;         current = pnext;         pnext = prev;         printf("交换后:current = %d,next = %d \n",current->data,current->next->data);     }     //printf("current = %d,next = %d \n",current->data,current->next->data);     L->next = current;  /* 将链表头节点指向p1 */     return L; }

  • 其实在你写函数的时候,我也写了个函数,也能运行。思路也差不多,不过你的current一直是表的第一个结点,我这里的current始终是首元结点的值,我的函数需要每次对pnext重新赋值。一会解释下。
 

Status ListReverse2(LinkList L) {     LinkList current, p;      if (L == NULL)     {         return NULL;     }     current = L->next;     while (current->next != NULL)     {         p = current->next;         current->next = p->next;         p->next = L->next;         L->next = p;     }     return L; }

  1. p = current->next; p 就相当于前面的 pnext。(图1中a2即为p)
  2. current->next = p->next; p->next 就相当于 prev的角色,这句代码意思是 current 的后继指向 prev.(相当于图1中a1->next = a3(a2->next))
  3. p->next = L->next; 这句就是 p 的后继直接指向首元节点。(相当于图1中a2->next = a1)
  4. L->next = p; 然后再将头结点指向 p。(相当于图1中L->next = a2

  • 参照图就很容易理解上面的步骤了。我觉得我这么写比你的清晰一些。我先将current指向prev,再将pnext指向current,最后将头结点指向pnext。

这个是程序运行的结果。

整体创建L的元素(头插法): // 原链表,current = 68, pnext = 55,68指向18,55指向18,头结点指向55 -> 68 -> 55 -> 18 -> 45 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67  // 第一次交换后,原链表变成这样 -> 55 -> 68 -> 18 -> 45 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 // 进行第二次交换,pnext = 18,68指向45,18变成头结点 -> 18 -> 55 -> 68 -> 45 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 // 进行第三次交换,pnext = current->next = 45,68指向41,45变成头结点 -> 45 -> 18 -> 55 -> 68 -> 41 -> 43 -> 5 -> 28 -> 80 -> 67 // …… -> 41 -> 45 -> 18 -> 55 -> 68 -> 43 -> 5 -> 28 -> 80 -> 67  -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 5 -> 28 -> 80 -> 67  -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 28 -> 80 -> 67  -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 80 -> 67  -> 80 -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68 -> 67 // current 68 没有后继,反转结束 -> 67 -> 80 -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68   反转L后 -> 67 -> 80 -> 28 -> 5 -> 43 -> 41 -> 45 -> 18 -> 55 -> 68

 

最后附上完整代码,反转有两个函数。

  • 方法1,current始终保持在第一位,pnext与prev遍历并完成交换。
  • 方法2,current始终是原链表的第一个数,然后把pnext不断移动到首位。

有两个方法可以实现单链表的反转:

方法一:

#include <stdio.h> #include <stdlib.h>  typedef struct Node {     int data;     struct Node *next;  }Node;  Node *head,*p;   Node * ReverseLink(Node *head) {     Node *p1, *p2, *p3;     if(head==NULL || head->next==NULL)         return head;     p1=head, p2=p1->next;     while(p2)     {         p3=p2->next;         p2->next=p1;         p1=p2;         p2=p3;     }     head->next=NULL;     head=p1;     return head; }  void CreateList(int n) {     Node *q;       int i;     printf("Input %2d data: ",n);      head=(Node *)malloc(sizeof(Node));      q=head;     scanf("%d",&q->data);     for(i=2;i<=n;i++)     {         q->next=(Node *)malloc(sizeof(Node));         q=q->next;         scanf("%d",&q->data);     }     q->next=NULL; }   void PrintList()  {      Node *q;      q=head;      while (q!=NULL)      {          printf("%-8d",q->data);         q=q->next;     }     printf("\n"); }  void main() {     CreateList(5);     PrintList();     head=ReverseLink(head);     PrintList(); }

方法二:

#include <iostream> #include <assert.h> using namespace std;   struct LNode{         char data;        LNode * next; };   LNode * initList() {        LNode *head=new LNode;      LNode *curPtr, *newPtr;       curPtr=head;        int i=0;         char ch='A';        while(i++<10)      {                 newPtr=new LNode;          newPtr->data=ch++;          curPtr->next=newPtr;         curPtr=newPtr;     }         newPtr->next=NULL;     return head; }   void print(LNode *head) {         LNode *ptr=head->next;     while(ptr != NULL)     {                 cout << ptr->data << "  ";         ptr=ptr->next;     }         cout << endl; }    void reverse(LNode *head) {        assert(head != NULL && head->next != NULL);     LNode *ptr=head->next->next;     head->next->next=NULL;          while(ptr != NULL)        {                LNode *tmp=ptr->next;             ptr->next=head->next;            head->next=ptr;                 ptr=tmp;     } }   int main() {        LNode *head=initList();       print(head);        cout << "After reverse: " << endl;      reverse(head);        print(head);          system("PAUSE");        return 0; }

参考:http://www.nowamagic.net/librarys/veda/detail/2241

            http://blog.csdn.net/hyg0811/article/details/11113623