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) { ListNode * pre=NULL; ListNode * current; while (head) { current = head; head = head->next; current->next=pre ; pre = current; } return pre; } };