Reverse a singly linked list.
/**
* 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(NULL == head || NULL == head->next) return head;
ListNode *p1 = head, *p2 = head->next;
p1->next = NULL;
while(NULL != p2){
ListNode* t = p2->next;
p2->next = p1;
p1 = p2;
p2 = t;
}
return p1;
}
};