zoukankan      html  css  js  c++  java
  • 链表反转

    #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;
    }

  • 相关阅读:
    socket 第一课
    _getitem__ __setitem__ __delitem__ __len__
    单继承&多继承 注意点
    面对对象 类&对象
    异常检测
    模块导入
    序列化模块注意点 json&pickle
    re模块
    filter和map
    Maven入门
  • 原文地址:https://www.cnblogs.com/cheng07045406/p/3286323.html
Copyright © 2011-2022 走看看