2023每日刷题(八十一)
Leetcode—92.反转链表II
算法思想
实现代码
/*** 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* reverseBetween(ListNode* head, int left, int right) {ListNode* dummy = new ListNode(0, head);ListNode* p0 = dummy;for(int i = 0; i < left - 1; i++) {p0 = p0->next;}ListNode* pre = nullptr, *cur = p0->next;for(int i = 0; i < right - left + 1; i++) {ListNode* nxt = cur->next;cur->next = pre;pre = cur;cur = nxt;}p0->next->next = cur;p0->next = pre;return dummy->next;}
};
运行结果
之后我会持续更新,如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持!