单链表反转
#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; }
posted on 2021-01-08 16:27 wlqsmiling 阅读(66) 评论(0) 编辑 收藏 举报