头插法。
/**
* Definition for singly-linked list.
* */
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
ListNode *reverseList(ListNode *head) {
ListNode *ans, *pre;
ans->next = NULL;
while (head != NULL) {
ListNode *node = new ListNode(head->val);
node->next = ans->next;
ans->next = node;
pre = head;
head = head->next;
delete pre;
}
return ans->next;
}
};