#include <iostream> using namespace std; struct Node { int i; struct Node *next; Node(int i1): i(i1), next(NULL) { } }; Node* reverse(Node* p) { Node *n, *h = NULL; while (p) { n = p->next; p->next = h; h = p; p = n; } return h; } void pr(Node *p) { while (p) { cout << p->i << ' '; p = p->next; } cout << endl; } int main() { Node n1(1), n2(2), n3(3); n1.next = &n2; n2.next = &n3; pr(&n1); Node *p = reverse(&n1); pr(p); return 0; }
单向链表反转
猜你喜欢
转载自blog.csdn.net/zhangxianww/article/details/80054976
今日推荐
周排行