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

单链表反转

时间:2021-01-11 11:11:15      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pac   using   cout   int   反转   reverse   div   stream   单链表反转   

#include <iostream>
#include <vector>
#include <string>

using namespace std;

struct Node {
    int data;
    Node * next;
};

Node * reverseList(Node * head) {
    Node * pre = nullptr;
    Node * cur = head;
    Node * next = nullptr;

    while (cur)
    {
        next = cur->next;
        cur->next = pre;
        pre = cur;
        cur = next;
    }

    return pre;
    
}

int main()
{
    Node * a = new Node();;
    a->data = 1;
    Node * b = new Node();
    b->data = 2;
    a->next = b;
    b->next = nullptr;

    
    Node * head =  reverseList(a);

    while (head) {
        cout<<head->data<<endl;
        head = head->next;
    }
    
    
    cout << "hello" << endl;

    return 0;
}

 

单链表反转

标签:pac   using   cout   int   反转   reverse   div   stream   单链表反转   

原文地址:https://www.cnblogs.com/wlqsmiling/p/14252079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!