【剑指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
- 【剑指offer】链表相关-合并两个有序链表&递归写法17
- 剑指offer--递归非递归合并两个有序链表
- 剑指offer----合并两个有序链表
- 《剑指offer》合并两个有序链表
- 剑指offer-合并两个有序链表
- 剑指offer 面试题17 合并两个有序链表
- 剑指Offer---面试题17:合并两个有序链表
- 有序链表合并的递归写法
- 递归实现合并两个有序链表
- 合并两个有序链表,递归实现
- 【剑指offer】合并两个有序的链表
- 剑指offer(8)-合并两个有序链表
- 3.剑指offer-合并两个有序的链表
- 剑指offer面试题-合并两个有序链表
- 剑指Offer-17-合并有序链表
- 合并两个有序链表的递归与非递归
- 合并两个有序链表(递归非递归方法)
- 合并两个有序链表(非递归与递归)
- windows环境下配置 scons 与 gnuwin32
- 单源最短路径-jobdu-1008
- void相关的无类型变量在使用时必须强制数据类型转换问题。
- 前三天的知识回顾
- LevelDb日知录(Leveldb 实现原理)
- 【剑指offer】链表相关-合并两个有序链表&递归写法17
- ios 最新真机调试
- SAX --- 处理 XML 文档
- poj 2761 Feed the dogs treap
- 最近又摆弄起C++
- nodejs的配置信息
- Xcode 5.0之后的改动
- 微信公众平台 创建自定义菜单
- 数据结构:线性表的链式存储(双向链表)--Java实现