206. 反转链表

pre=NULL,now=head,while(now!=NULL)

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode() : val(0), next(nullptr) {}
  7. * ListNode(int x) : val(x), next(nullptr) {}
  8. * ListNode(int x, ListNode *next) : val(x), next(next) {}
  9. * };
  10. */
  11. class Solution {
  12. public:
  13. ListNode* reverseList(ListNode* head) {
  14. ListNode* pre = nullptr;
  15. ListNode* now = head;
  16. while(now!=nullptr){
  17. ListNode* next = now->next;
  18. now->next = pre;
  19. pre = now;
  20. now = next;
  21. }
  22. return pre;
  23. }
  24. };