标签:public linked src else ptr onclick node cout lap
链接:https://leetcode-cn.com/problems/reverse-linked-list/
代码:
/** * 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) { if(head == nullptr) return nullptr; ListNode* cur = head; ListNode* ret = nullptr; while(cur) { if(ret == nullptr) { ListNode* temp = new ListNode(cur->val); ret = temp; cout << cur->val << endl; } else { ListNode* temp = new ListNode(cur->val); temp->next = ret; ret = temp; cout << cur->val << endl; } cur = cur->next; } return ret; } };
思路:头插。
标签:public linked src else ptr onclick node cout lap
原文地址:https://www.cnblogs.com/FriskyPuppy/p/12953506.html