【剑指offer】链表相关-合并两个有序链表&递归写法17

来源:互联网 发布:千牛卖家版mac不能用 编辑:程序博客网 时间:2024/05/01 07:23
#include<iostream.h>#include <stdio.h>#include <stack>//合并两个有序链表struct ListNode{int m_nValue;ListNode * m_pNext;};ListNode * CreateLink(int a[],int k){ListNode * Head=NULL,*q=NULL;for(int i=0;i<k;i++){ListNode * pNew=new ListNode();        pNew->m_nValue=a[i];       pNew->m_pNext=NULL;if(Head==NULL){Head=pNew;q=pNew;}else{q->m_pNext=pNew;q=q->m_pNext;}}return Head;}//从头到尾打印列表void printLink(ListNode * pHead){cout<<"链表内容为:";ListNode *p=pHead;while(p){cout<<p->m_nValue<<" ";p=p->m_pNext;}cout<<endl;}//自己写的程序ListNode * Merge1(ListNode * pHead1,ListNode * pHead2){ListNode * newList=NULL,*p=NULL;if(pHead1==NULL && pHead2==NULL)return newList;if(pHead1==NULL)return pHead2;if(pHead2==NULL)return pHead1;while(pHead1!=NULL && pHead2!=NULL){if(pHead1->m_nValue >= pHead2->m_nValue ){//处理新链表的第一个结点if(newList==NULL){newList=pHead2;pHead2=pHead2->m_pNext;p=newList;p->m_pNext=NULL;}else{p->m_pNext=pHead2;pHead2=pHead2->m_pNext;p=p->m_pNext;p->m_pNext=NULL;}}else{            //处理新链表的第一个结点if(newList==NULL){newList=pHead1;pHead1=pHead1->m_pNext;p=newList;p->m_pNext=NULL;}else{p->m_pNext=pHead1;pHead1=pHead1->m_pNext;p=p->m_pNext;p->m_pNext=NULL;}}}if(pHead1==NULL){p->m_pNext=pHead2;}if(pHead2==NULL){p->m_pNext=pHead1;}return newList;}//书上的ListNode * Merge(ListNode * pHead1,ListNode * pHead2){if(pHead1 == NULL)return pHead2;if(pHead2 == NULL)return pHead1;ListNode * pMergedHead=NULL;if(pHead1->m_nValue < pHead2->m_nValue ){pMergedHead=pHead1;pMergedHead->m_pNext=Merge(pHead1->m_pNext,pHead2);}else{pMergedHead=pHead2;pMergedHead->m_pNext=Merge(pHead2->m_pNext,pHead1);}return pMergedHead;}//=======测试用例=========//1.合并两个空链表//2.其中一个为空链表//3.其中一个为一个节点void Test1(){cout<<"测试用例test1"<<endl;ListNode * ptr1=NULL;ListNode * ptr2=NULL;printLink(ptr1);printLink(ptr2);ListNode * newList=Merge(ptr1,ptr2);printLink(newList);}void Test2(){cout<<"测试用例test2"<<endl;int a[]={1,2,3,4};ListNode * ptr1=CreateLink(a,4);ListNode * ptr2=NULL;printLink(ptr1);printLink(ptr2);ListNode * newList=Merge(ptr1,ptr2);printLink(newList);}void Test3(){cout<<"测试用例test3"<<endl;int a[]={1,2,3,4};ListNode * ptr1=NULL;ListNode * ptr2=CreateLink(a,4);printLink(ptr1);printLink(ptr2);ListNode * newList=Merge(ptr1,ptr2);printLink(newList);}void Test4(){cout<<"测试用例test4"<<endl;int a[]={1,2,3,4};int b[]={2,4,6,7};ListNode * ptr1=CreateLink(a,4);ListNode * ptr2=CreateLink(b,4);printLink(ptr1);printLink(ptr2);ListNode * newList=Merge(ptr1,ptr2);printLink(newList);}void Test5(){cout<<"测试用例test5"<<endl;int a[]={1,2,3,4};int b[]={2};ListNode * ptr1=CreateLink(a,4);ListNode * ptr2=CreateLink(b,1);printLink(ptr1);printLink(ptr2);ListNode * newList=Merge(ptr1,ptr2);printLink(newList);}void Test6(){cout<<"测试用例test6"<<endl;int a[]={1,2,3,4};int b[]={2,2,2};ListNode * ptr1=CreateLink(a,4);ListNode * ptr2=CreateLink(b,3);printLink(ptr1);printLink(ptr2);ListNode * newList=Merge(ptr1,ptr2);printLink(newList);}void main(){Test1();Test2();Test3();Test4();Test5();Test6();}

0 0