From : https://leetcode.com/problems/reverse-linked-list/
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 *cur=head,*res = NULL;
while(cur) {
ListNode* nxt = cur->next;
cur->next = res;
res = cur;
cur = nxt;
}
return res;
}
};