Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

Reverse Linked List II - กลับทิศทางโหนดระหว่างตำแหน่ง m ถึง n ใน C++


ภาพรวมปัญหา

มี Linked List ต้องกลับทิศทางโหนดตั้งแต่ตำแหน่ง m ถึง n (1-indexed) ทำในหนึ่งครั้ง (one pass)

ตัวอย่าง: [1,2,3,4,5], m=2, n=4 → [1,4,3,2,5]

อัลกอริทึม (Recursive)

ใช้สองฟังก์ชัน: reverseN() และ reverseBetween()

  • reverseN(head, n): กลับ n โหนดแรก
    • ถ้า n=1: successor = head->next, return head
    • last = reverseN(head->next, n-1)
    • head->next->next = head, head->next = successor, return last
  • reverseBetween(head, m, n):
    • ถ้า m=1: return reverseN(head, n)
    • head->next = reverseBetween(head->next, m-1, n-1)
    • return head

ตัวอย่างโค้ด C++

#include <bits/stdc++.h>
using namespace std;

class ListNode{
public:
    int val;
    ListNode *next;
    ListNode(int data){ val = data; next = NULL; }
};

ListNode *make_list(vector<int> v){
    ListNode *head = new ListNode(v[0]);
    for(int i = 1; i < v.size(); i++){
        ListNode *ptr = head;
        while(ptr->next != NULL) ptr = ptr->next;
        ptr->next = new ListNode(v[i]);
    }
    return head;
}

void print_list(ListNode *head){
    ListNode *ptr = head;
    cout << "[";
    while(ptr){ cout << ptr->val << ", "; ptr = ptr->next; }
    cout << "]" << endl;
}

class Solution {
public:
    ListNode* successor = NULL;
    ListNode* reverseN(ListNode* head, int n){
        if(n == 1){
            successor = head->next;
            return head;
        }
        ListNode* last = reverseN(head->next, n - 1);
        head->next->next = head;
        head->next = successor;
        return last;
    }
    ListNode* reverseBetween(ListNode* head, int m, int n){
        if(m == 1) return reverseN(head, n);
        head->next = reverseBetween(head->next, m - 1, n - 1);
        return head;
    }
};

int main(){
    Solution ob;
    vector<int> v = {1,2,3,4,5,6,7,8};
    ListNode *head = make_list(v);
    print_list(ob.reverseBetween(head, 2, 6));
}

Input / Output

Input:  [1,2,3,4,5,6,7,8] 2 6
Output: [1, 6, 5, 4, 3, 2, 7, 8]