思路很快就有了:非递归的思路,这回和上回有点区别,但是都通过啦 开心
/**
* 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) return head;
ListNode* newhead=NULL;
ListNode* oldhead=head;
ListNode* tem=oldhead->next;
while(tem)
{
oldhead->next = newhead;
newhead = oldhead;
oldhead = tem;
tem = tem->next;
}
oldhead->next = newhead;
newhead = oldhead;
return newhead;
}
};