华为机试—链表翻转(给出链表和数K)

题目:

给出一个链表和一个数k,比如链表1→2→3→4→5→6,k=2,则翻转后2→1→4→3→6→5,若k=3,翻转后3→2→1→6→5→4,若k=4,翻转后4→3→2→1→5→6


基本思想:

链表中长度为k的一段从链表中摘除,翻转之后在将其衔接回链表。


    #include<iostream>  
    using namespace std;  
      
    typedef struct LNode {  
        int m_nValue;  
        struct LNode* m_pNext;  
    }LNode;  
      
    LNode* reverseList(LNode* pHead) {  
        if (NULL == pHead || NULL == pHead->m_pNext)  
            return pHead;  
        LNode* pNode;  
        LNode* pNewHead = NULL;  
        while (pHead) {  
            pNode = pHead;  
            pHead = pHead->m_pNext;  
            pNode->m_pNext = pNewHead;  
            pNewHead = pNode;  
        }  
        return pNewHead;  
    }  
      
    LNode* getLastNode(LNode* pHead) {  
        while (NULL != pHead->m_pNext)  
            pHead = pHead->m_pNext;  
        return pHead;  
    }  
      
    LNode* swapListByK(LNode* pHead, int k) {  
        if (k <= 1)  
            return pHead;  
        int pos;  
        LNode* pNode = pHead;  
        LNode* pNewHead;  
        LNode* pNextNode;  
        LNode* pLastNode = NULL;;  
        pHead = NULL;  
        while (pNode) 
		{  
            pos = 0;  
            pNewHead = pNode;  
            while (pNode && pos < k - 1) {  
                pNode = pNode->m_pNext;  
                pos++;  
            }  

            if (pNode)
			{  
                pNextNode = pNode->m_pNext;  
                pNode->m_pNext = NULL;  
                if (NULL != pLastNode) 
				{  
                    pLastNode->m_pNext = NULL;  
                }  
                pNewHead = reverseList(pNewHead);  
                if (NULL == pHead) 
                    pHead = pNewHead; 
				else
                    pLastNode->m_pNext = pNewHead;

                pNode = getLastNode(pNewHead);  
                pNode->m_pNext = pNextNode;  
                pLastNode = pNode;  
                pNode = pNextNode;  
            }
			else 
                break;  
        }  
        return pHead;  
    }  
      
    void printList(LNode* pHead) {  
        LNode* pNode = pHead;  
        while (pNode) {  
            cout << pNode->m_nValue << " ";  
            pNode = pNode->m_pNext;  
        }  
        cout << endl;  
    }  
      
    LNode* createList(int* arr, int length) {  
        LNode* pHead = NULL;  
        LNode* pTemp, *pNode;  
        for (int i = 0; i < length; i++) {  
            pNode = (LNode*)malloc(sizeof(LNode));  
            pNode->m_nValue = arr[i];  
            pNode->m_pNext = NULL;  
            if (NULL == pHead)  
                pHead = pNode;  
            else  
                pTemp->m_pNext = pNode;  
            pTemp = pNode;  
        }  
        return pHead;  
    }  
      
    void destroyList(LNode* pHead) {  
        LNode* pNode;  
        while (pHead) {  
            pNode = pHead;  
            pHead = pHead->m_pNext;  
            free(pNode);  
        }  
    }  
      
    int main() 
	{  
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};  
        int length = sizeof(arr) / sizeof(arr[0]);  
        LNode* pHead = createList(arr, length);  

        pHead = swapListByK(pHead, 2);  

        printList(pHead);  
        destroyList(pHead);   
		
        return 0;  
    }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值