比较简洁高效的程序:
/**
* 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) {
if(!head || !head->next) return head;
auto second = head->next;
ListNode* third = NULL;
head->next = NULL;
while(second)
{
third = second->next;
second->next = head;
head = second;
second = third;
}
return head;
}
};