将单向链表反转
完成如图操作,依次进行即可
1
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) {
ListNode* now = head;
head = NULL;
while(now){
ListNode* tnow= now;
now = now->next;
tnow->next = head;
head = tnow;
}
return head;
}
};