将单向链表反转
完成如图操作,依次进行即可
1
2
3
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* reverseList(ListNode* head) { 12 ListNode* now = head; 13 head = NULL; 14 while(now){ 15 ListNode* tnow= now; 16 now = now->next; 17 tnow->next = head; 18 head = tnow; 19 } 20 return head; 21 } 22 };