Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 *start, *pre_ptr, *next_ptr;
- ListNode *dummy = new ListNode(0, head);
- ListNode *curr=dummy;
- for(int i=0; i<left-1; i++){
- curr = curr->next;
- }
- pre_ptr = curr;
- start = curr->next;
- next_ptr = start->next;
- for(int i=0; i<right-left; i++){
- start->next = next_ptr->next;
- next_ptr->next = pre_ptr->next;
- pre_ptr->next = next_ptr;
- next_ptr = start->next;
- }
- return dummy->next;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement