Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 594 Bytes

92. Reverse Linked List II.md

File metadata and controls

26 lines (21 loc) · 594 Bytes
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int left, int right) {
        ListNode dummy(-1);
        dummy.next = head;
        ListNode* pre = &dummy;
        
        for (int i = 0; i < left - 1; ++i) {
            pre = pre->next;
        }
        
        ListNode* cur = pre->next;
        
        for (int i = 0; i < right - left; ++i) {
            ListNode* temp = pre->next;
            pre->next = cur->next;
            cur->next = cur->next->next;
            pre->next->next = temp;
        }
        
        return dummy.next;
    }
};