题目描述

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

解题思路

  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_node = NULL;
  15. ListNode* curr = head;
  16. while (curr) {
  17. ListNode* temp = curr->next;
  18. curr->next = pre_node;
  19. pre_node = curr;
  20. curr = temp;
  21. }
  22. return pre_node;
  23. }
  24. };