力扣206. 反转链表
经典题目,也比较简单,不多说,记录一下自己的思路
题目:【https://leetcode.cn/problems/reverse-linked-list/】
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 if (!head || !head->next) 15 return head; 16 17 ListNode* f = head; 18 ListNode* s = head->next; 19 ListNode* t = NULL; 20 for (; s;) { 21 t = s->next; 22 s->next = f; 23 f = s; 24 s = t; 25 } 26 head->next = NULL; 27 return f; 28 } 29 };