单链表反转

#include <iostream>

struct Node
{
    int m_iData;
    Node* m_pNext;
};

Node* reverseList(Node* head)
{
    if(NULL == head)
    {
        return NULL;
    }
    
    Node* a = head;
    if(NULL == a->m_pNext)
    {
        return a;
    }
    
    Node* b = a->m_pNext;
    if(NULL == b->m_pNext)
    {
        a->m_pNext = NULL;
        b->m_pNext = a;
        return b;
    }
    
    Node* c = b->m_pNext;
    if(NULL == c->m_pNext)
    {
        a->m_pNext = NULL;
        b->m_pNext = a;
        c->m_pNext = b;
        return c;
    }
    
    a->m_pNext = NULL;
    
    do{
        b->m_pNext = a;
        a = b;
        b = c;
        c = c->m_pNext;        
    }while(NULL != c->m_pNext);
    
    b->m_pNext = a;
    c->m_pNext = b;
    
    return c;
}

void printNodeList(const Node* head) 
{
    if(NULL == head)
    {
        printf("[0]NULL\n");
        return;
    }
    
    int count = 0;    
    while(NULL != head)
    {
        printf("[%d]%d -> ", count, head->m_iData);
        head = head->m_pNext;
        ++count;
    }    
    printf("NULL\n");
}

Node* initNodeList(const int* initList, int iLen)
{
    Node* head = NULL;    
    if(NULL != initList)
    {
        if(0 < iLen)
        {            
            head = new Node;
            head->m_iData = initList[0];
            
            Node* oT1 = head;
            Node* oT2 = NULL;
            
            for(int i = 1; i < iLen; ++i)
            {
                oT2 = new Node;
                oT2->m_iData = initList[i];

                oT1->m_pNext = oT2;
                oT1 = oT2;
            }
            
            oT1->m_pNext = NULL;
        }
    }

    return head;
}

void delNodeList(Node* head)
{
    if(NULL != head)
    {
        Node* oT = NULL;
        while(NULL != head)
        {
            oT = head;
            head = head->m_pNext;
            
            oT->m_pNext = NULL;
            delete oT;
            oT = NULL;
        }
    }
}

void conFun(int* iArr, int iLen)
{
    Node* pNode = NULL;
    pNode = initNodeList(iArr, iLen);
    printNodeList(pNode);
    pNode = reverseList(pNode);
    printNodeList(pNode);
    delNodeList(pNode);
}

int main(int argc, const char * argv[])
{   
    int iArr1[] = {1, 2, 3, 4, 5, 6};
    conFun(iArr1, sizeof(iArr1) / sizeof(int));
    
    int iArr2[] = {};
    conFun(iArr2, sizeof(iArr2) / sizeof(int));

    int iArr3[] = {1};
    conFun(iArr3, sizeof(iArr3) / sizeof(int));    
    
    int iArr4[] = {1, 2};
    conFun(iArr4, sizeof(iArr4) / sizeof(int));
    
    int iArr5[] = {1, 2, 3};
    conFun(iArr5, sizeof(iArr5) / sizeof(int));    
    
    int iArr6[] = {1, 2, 3, 4};
    conFun(iArr6, sizeof(iArr6) / sizeof(int));   
    
    return 0;
}

输出:

[0]1 -> [1]2 -> [2]3 -> [3]4 -> [4]5 -> [5]6 -> NULL
[0]6 -> [1]5 -> [2]4 -> [3]3 -> [4]2 -> [5]1 -> NULL
[0]NULL
[0]NULL
[0]1 -> NULL
[0]1 -> NULL
[0]1 -> [1]2 -> NULL
[0]2 -> [1]1 -> NULL
[0]1 -> [1]2 -> [2]3 -> NULL
[0]3 -> [1]2 -> [2]1 -> NULL
[0]1 -> [1]2 -> [2]3 -> [3]4 -> NULL
[0]4 -> [1]3 -> [2]2 -> [3]1 -> NULL

你可能感兴趣的:(单链表反转)