#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode* reverseList(ListNode* l) {
ListNode* pre = NULL, * temp = NULL;
ListNode* cur = l;
while (cur) {
//store the next node
temp = cur->next;
//reverse
cur->next = pre;
//move pointers
pre = cur;
cur = temp;
}
return pre;
}
};
int main() {
ListNode* l1 = new ListNode(2);
l1->next = new ListNode(9);
l1->next->next = new ListNode(1);
ListNode* ans = Solution().reverseList(l1);
while (ans) {
cout << ans->val << " ";
ans = ans->next;
}
return 0;
}