---
title: "C++合并链表"
author: "rayloo"
date: "2008-02-13T15:10:00Z"
tags: []
slug: "c"
draft: false
lastmod: "2008-02-13T15:10:00Z"
id: 1008874
url: "https://rayloo.iteye.com/blog/1008874"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)voidMergeList(LinkListLa,LinkList\*Lb,LinkList\*Lc)/\*\*//\*算法2.12\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{/\*\*//\*已知单链线性表La和Lb的元素按值非递减排列。\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)/\*\*//\*归并La和Lb得到新的单链线性表Lc，Lc的元素也按值非递减排列\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)LinkListpa=La-&gt;next,pb=(\*Lb)-&gt;next,pc;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)\*Lc=pc=La;/\*\*//\*用La的头结点作为Lc的头结点\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)while(pa&&pb)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(pa-&gt;data&lt;=pb-&gt;data)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pc-&gt;next=pa;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pc=pa;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pa=pa-&gt;next;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)else  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pc-&gt;next=pb;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pc=pb;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)pb=pb-&gt;next;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)pc-&gt;next=pa?pa:pb;/\*\*//\*插入剩余段\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)free(\*Lb);/\*\*//\*释放Lb的头结点\*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Lb=NULL;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

上次微软电话面试，我没有回答上来。。。呜呜  
注： LinkListLa,LinkList\*Lb,LinkList\*Lc 加了\*表示 后来发生变化而已。