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

    #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;
    }
  • 相关阅读:
    4月24日 PHP基础
    4月22日 常用函数
    4月22日 练习题
    PHP正则数组
    PHP基础函数应用
    数据库SQL语句
    高级查询
    mysql
    CSS样式表
    词汇
  • 原文地址:https://www.cnblogs.com/wlqsmiling/p/14252079.html
Copyright © 2011-2022 走看看