相关题目:92. 反转链表 II - 力扣(LeetCode)
解法:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head)
{
ListNode * pre = nullptr;
ListNode * cur = head;
while (cur != nullptr) {
ListNode * post = cur->next;
cur->next = pre;
pre = cur;
cur = post;
}
return pre;
}
};
总结:
计算时间复杂度O(N),空间复杂度O(1)。相关题目解法:反转链表2(92)-优快云博客

被折叠的 条评论
为什么被折叠?



