据说是一道挺重要的题,一定背下来嗯……
/**
* 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* p = head;
ListNode* prev = NULL;
ListNode* temp;
while(p){
temp = p->next;
p->next = prev;
prev = p;
p = temp;
}
return prev;
}
};
python的实现和cpp没什么区别, 就不贴了