码迷,mamicode.com
首页 > 其他好文 > 详细

Reverse链表 递归实现

时间:2016-08-19 06:17:15      阅读:129      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!