首页 > 其他 > 详细

Reverse链表 递归实现

时间:2016-08-19 06:16:10      阅读:99      评论:0      收藏:0      [点我收藏+]
#include<iostream>

struct node{
  int payload;
  node* next;
};
void bianli(node* head){
  node* iterator = head;
  while(iterator){
    std::cout << iterator->payload<<" ";
    iterator = iterator->next;
  }
  std::cout<<" "<<std::endl;
}

node* diguifunc(node* head){
    if(head==nullptr|| head->next==nullptr)
        return head;
    
    node* second = head->next;
    node* new_head = diguifunc(second);
    second->next = head;
    head->next = nullptr;
    return new_head;
}

int main(){
    node* head = nullptr;
    for(int i=0;i<10;i++){
        node* new_node = new node;
        new_node->payload = i*10;
        new_node->next=head;
        head = new_node;
    }
    head = diguifunc(head);
    bianli(head);

    system("pause");
    return 0;
}

 

Reverse链表 递归实现

原文:http://www.cnblogs.com/zychengzhiit1/p/5786096.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!