1.描述
Reverse a singly linked list.
2.分析
3.代码
/**
* 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 == NULL || head->next == NULL) return head;
ListNode* cur = head;
ListNode* pre;
head = NULL;
while (cur != NULL) {
pre = cur;
cur = cur->next;
pre->next = head;
head = pre;
}
return head;
}
};