标签:
/**
* 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)
return NULL;
ListNode *nhead = NULL;//nhead来保存一个新的链表
while(head)
{ListNode*cur = head;//处理的时候cur 是当前处理的节点,
head= head->next;
cur->next = nhead;//nhead是当前节点的前一个节点
nhead = cur;//为下次循环服务
}
return nhead;
}
};
标签:
原文地址:http://www.cnblogs.com/gofighting/p/5036143.html