链表指定区域反转

class Solution {
public:
    /*
        this function is used to reverse the list in the given range[m,n]
    */
    ListNode* reverseBetween(ListNode* head, int m, int n)
    {
        int times = n - m;
        int size = 0;
        ListNode* cur = head;
        while (cur != nullptr)
        {
            size++;
            cur = cur->next;
        }
        //cout << size;
        if (size == 1 ||m==n)
        {
            return head;
        }
        if (size == 2)
        {
            ListNode* phead = head;
            ListNode* ptail = head->next;
            head = ptail;
            head->next = phead;
            phead->next = nullptr;
            return head;
        }
        
        
        ListNode* pre_phead = head;
        ListNode* phead = nullptr;
        ListNode* next_phead = nullptr;
        ListNode* ptail = head;
        ListNode* next_ptail = nullptr;
        bool ifOne = false;
        if (m == 1)
        {
            phead = head;
            //next_phead = phead->next;
            ifOne = true;
        }
        else
        {
            for (int i = 0; i < m - 2; i++)
            {
                pre_phead = pre_phead->next;   // no change
            }
            phead = pre_phead->next; // need change
            
        }
        next_phead = phead->next; // need change
        for (int i = 0; i < n-1; i++)
        {
            ptail = ptail->next; //need change
        }
        next_ptail = ptail->next; // no change
        //cout << phead->val << " " << ptail->val << endl; //test:right
        int flag = 0;
        if (ifOne == true)
        {
            for (int i = 0; i < times; i++)
            {
                    ptail->next = phead;
                    phead->next = next_ptail;
                    ptail = phead;
                    phead = next_phead;
                    next_phead = next_phead->next;
                    flag = 1;
            }
            head = phead;
            pre_phead = head;
            phead = phead->next;
            next_phead = next_phead->next;
        }

        for (int i = 0; i <times-flag; i++)
        {
            //reverseBetween(head, m, n);
            for (int j = times - i-flag; j > 0; j--)
            {
                
                pre_phead->next = next_phead;
                ptail->next = phead;
                phead->next = next_ptail;
                ptail = phead;
                phead = next_phead;
                next_phead = next_phead->next;
            }
            pre_phead = pre_phead->next;
            phead = phead->next;
            next_phead = next_phead->next;
        }
        
        return head;
    }
};

测试代码

#include "iostream"
#include"stack"
using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(nullptr) {}
};

void Print(ListNode* head) {
    ListNode* cur = head;
    while (cur != nullptr) {
        std::cout << cur->val << " ";
        cur = cur->next;
    }
    std::cout << std::endl;
}


void ListPushBack(ListNode** head, int data) {
    if (*head == nullptr) {
        *head = new ListNode(data);
        return;
    }

    ListNode* cur = *head;
    while (cur->next != nullptr) {
        cur = cur->next;
    }
    cur->next = new ListNode(data);
}


int main() {
    /*ListNode* head = nullptr;
    ListPushBack(&head, 1);
    ListPushBack(&head, 2);
    ListPushBack(&head, 3);
    Print(head);*/

    /*Solution solution;
    ListNode* newhead = solution.ReverseList(head);
    Print(newhead);*/

    /*SolutionStack solutionStack;
    ListNode* newHead2 = solutionStack.ReverseList(&head);
    Print(newHead2);*/

    ListNode* head1 = nullptr;
    ListNode* head2 = nullptr;
    ListNode* head3 = nullptr;
    ListNode* head4 = nullptr;
    ListNode* head5 = nullptr;
    for (int i = 0; i < 10; i++)
    {
        ListPushBack(&head1, i + 1);
        ListPushBack(&head2, i + 1);
        ListPushBack(&head3, i + 1);
        ListPushBack(&head4, i + 1);
        ListPushBack(&head5, i + 1);
    }
    cout << "链表数据:";
    Print(head1);

    Solution solution;
    ListNode* newHead_1 = solution.reverseBetween(head1,1,10); //全反转
    cout << "全反转:";
    Print(newHead_1);

    ListNode* newHead_2 = solution.reverseBetween(head2, 3, 3);  // 不反转
    cout << "不反转:";
    Print(newHead_2);

    ListNode* newHead_3 = solution.reverseBetween(head3, 1, 5); //从开头开始反转
    cout << "从开头开始反转:";
    Print(newHead_3);

    ListNode* newHead_4 = solution.reverseBetween(head4, 3, 10);  // 反转到结尾
    cout << "反转到结尾:";
    Print(newHead_4);

    ListNode* newHead_5 = solution.reverseBetween(head5, 3, 7); //中间反转
    cout << "中间反转:";
    Print(newHead_5);
    return 0;
}

结果

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值