链表反转

来源:互联网 发布:licecap mac版 编辑:程序博客网 时间:2024/06/06 15:41
void reverse(Node*& head){if(head==NULL)return ;Node* pre,*cur,*p;pre=head;cur=head->next;while(cur){p=cur->next;cur->next=pre;pre=cur;cur=p;}head->next=NULL;head=pre;}Node* reverse(Node* p,Node* &head){if (p==NULL||p->next==NULL){head->next=0;head=p;return p;}Node* temp=reverse(p->next,head);temp->next=p;return p; }void main(){Node* head,*p;int temp;head=new Node();    p=head;while (cin>>temp){        Node* cur=new Node(temp);        p->next=cur;p=cur;  }   cin.clear();   p=head->next;    //reverse(p);   Node* q=reverse(p,p);   while(p)   {   cout<<p->data<<" ";   p=p->next;   }   cin.get();}

  反转链表_递归与非递归

链表反转 递归和非递归版

  单向链表的反转