zoukankan      html  css  js  c++  java
  • 合并两个排序的链表(非递归)

    输入两个递增排序的链表,合并这两个链表并使新链表中的结构任然递增!

    1 struct ListNode 
    2 {
    3     int m_nValue;
    4     ListNode* m_pNext;
    5 };
     1 ListNode* CreatList(int* data , unsigned int length)
     2 {
     3     if (!data || length<=0)
     4     {
     5         return NULL;
     6     }
     7 
     8     ListNode* pHeadNode = new ListNode();
     9     pHeadNode->m_nValue = data[0] ;
    10     pHeadNode->m_pNext = NULL ;
    11     ListNode* pAhead = pHeadNode ;
    12     for (int i=1 ; i!= length ;i++)
    13     {
    14         ListNode* pNode = new ListNode();
    15         pNode->m_nValue = data[i];
    16         pNode->m_pNext = NULL;
    17         pAhead->m_pNext = pNode ;
    18         pAhead = pNode ;
    19 
    20     }
    21     pAhead = NULL ;
    22     return pHeadNode ;
    23 }
     1 void PrintList(ListNode* pHead )
     2 {
     3     if (!pHead)
     4     {
     5         return;
     6     }
     7     else
     8     {
     9         while (pHead->m_pNext != NULL)
    10         {
    11             cout<<pHead->m_nValue<<" ";
    12             pHead = pHead->m_pNext ;
    13         }
    14         cout<<pHead->m_nValue<<endl;
    15     }
    16 
    17 }
    ///////////////////合并两个排序的链表////////////////////
    ListNode* Merge(ListNode* pHead1 ,ListNode* pHead2)
    {
        if (!pHead1) return pHead2;
        if (!pHead2) return pHead1;
        if (!pHead1 && !pHead2) return NULL ;
        
        ListNode* pHead = NULL ;
        ListNode* pNext = NULL;
        ListNode* pNode = NULL ;
        if (pHead1->m_nValue <= pHead2->m_nValue)
        {
            pHead = pHead1 ;
            pNode = pHead2 ;
        }else
        {
            pHead = pHead2 ;
            pNode = pHead1 ;
        }
        ListNode* pMergedHead = pHead ;
        while(pNode != NULL)
        {
            while((pHead->m_pNext !=NULL)  &&  (pHead->m_pNext->m_nValue) <= (pNode->m_nValue) ) 
            {
                pHead = pHead->m_pNext ;
            }
            pNext = pHead->m_pNext ;
            pHead->m_pNext = pNode ;
            pHead = pNode ;
            pNode = pNext ;
    
        }
    
        return pMergedHead ;
    
    }
     1 int main()
     2 {
     3     int iv1[] = {1,3,7,7,9};
     4     int iv2[] = {0,2,4,6,10};
     5     
     6     ListNode* pHead1 = CreatList(iv1,5);
     7     ListNode* pHead2 = CreatList(iv2,5);
     8     PrintList(pHead1);
     9     PrintList(pHead2);
    10     
    11     ListNode* pMergedHead = Merge(pHead1,pHead2);
    12     cout<<"合并后:";
    13     PrintList(pMergedHead);
    14 
    15     delete pMergedHead ;
    16  
    17     system("pause");
    18     return 0;
    19 }
  • 相关阅读:
    Java实现自定义排序
    常用加密算法
    隐式传参
    mybatis-plus多租户的使用
    Python3.x基础学习-类--面向对象
    Python3.x基础学习-函数用法(四)
    Python3.x基础学习-函数用法(三)
    Python3.x基础学习-函数用法(二)
    功能测试经验汇总(--持续更新)
    Python3.x基础学习-函数用法(一)
  • 原文地址:https://www.cnblogs.com/csxcode/p/3690304.html
Copyright © 2011-2022 走看看