按左右半区的方式重新组合单链表


#include "List.h"
using namespace std;
void mergeLR(Node* left, Node* right)
{
    Node* next = nullptr;
    while(left->next != nullptr)
    {
        next = right->next;
        right->next = left->next;
        left->next = right;
        left = right->next;
        right = next;
    }
    left->next = right;
}

void relocate(Node* head)
{
    if(head == nullptr || head->next == nullptr)
        return;
    Node* mid = head;
    Node* right = mid->next;
    while(right->next && right->next->next)
    {
        mid = mid->next;
        right = right->next->next;
    }
    right = mid->next;
    mid->next = nullptr;
    mergeLR(head, right);
}

int main()
{
    Node* pNode0 = new Node(0);
    Node* pNode1 = new Node(1, pNode0);
    Node* pNode2 = new Node(2, pNode1);
    Node* pNode3 = new Node(2, pNode2);
    Node* pNode4 = new Node(1, pNode3);
    Node* pNode5 = new Node(0, pNode4);

    relocate(pNode5);
    Print(pNode5);
}

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: