网站转发,广东建设信息网站,公司做网站需要什么资料,北京网络公司注册原题链接#xff1a;
合并两个排序的链表_牛客题霸_牛客网
思路分析#xff1a;
第一步#xff1a;写一个链表尾插数据的方法。
typedef struct ListNode ListNode;//申请结点
ListNode* BuyNode(int x)
{ListNode* node (ListNode*)malloc(sizeof(ListNode));node-
合并两个排序的链表_牛客题霸_牛客网
思路分析
第一步写一个链表尾插数据的方法。
typedef struct ListNode ListNode;//申请结点
ListNode* BuyNode(int x)
{ListNode* node (ListNode*)malloc(sizeof(ListNode));node-val x;node-next NULL;return node;
}//尾插
void ListPushBack(ListNode** pphead, int x)
{ListNode* NewNode BuyNode(x);if(*pphead NULL){*pphead NewNode;}else {ListNode* pcur *pphead;while(pcur-next){pcur pcur-next;}pcur-next NewNode;}
}
第二步创建三个链表结点。l1 用来遍历链表1l2用来遍历链表2RetList 是返回链表。
ListNode* RetList NULL, *l1 pHead1, *l2 pHead2;
第三步进行遍历判断。如果 l1 结点里的值小于 l2 结点里的值就把 l1 结点里的值尾插到返回链表。反之就把 l2 结点里是值尾插到返回链表。 while(l1 l2){if(l1-val l2-val){ListPushBack(RetList, l1-val);l1 l1-next;}else{ListPushBack(RetList, l2-val);l2 l2-next;}}
循环之前 第一次循环之后 第二次循环之后 第三次循环之后 第四次循环之后 第五次循环之后 如果 l1 或者 l2 遍历到 NULL 的时候会有一方没有完全遍历完所有结点所以我们还需要补上两个循环去遍历完 l1 或者 l2。 while(l1){ListPushBack(RetList, l1-val);l1 l1-next;}while(l2){ListPushBack(RetList, l2-val);l2 l2-next;}return RetList;
完整代码
struct ListNode* Merge(struct ListNode* pHead1, struct ListNode* pHead2 )
{ListNode* RetList NULL, *l1 pHead1, *l2 pHead2;while(l1 l2){if(l1-val l2-val){ListPushBack(RetList, l1-val);l1 l1-next;}else{ListPushBack(RetList, l2-val);l2 l2-next;}}while(l1){ListPushBack(RetList, l1-val);l1 l1-next;}while(l2){ListPushBack(RetList, l2-val);l2 l2-next;}return RetList;
} 文章转载自: http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.flzqq.cn.gov.cn.flzqq.cn http://www.morning.fhykt.cn.gov.cn.fhykt.cn http://www.morning.xrsqb.cn.gov.cn.xrsqb.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.ypdhl.cn.gov.cn.ypdhl.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.snrbl.cn.gov.cn.snrbl.cn http://www.morning.slzkq.cn.gov.cn.slzkq.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.lwqst.cn.gov.cn.lwqst.cn http://www.morning.nwjzc.cn.gov.cn.nwjzc.cn http://www.morning.pbsfq.cn.gov.cn.pbsfq.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.fosfox.com.gov.cn.fosfox.com http://www.morning.splkk.cn.gov.cn.splkk.cn http://www.morning.bftqc.cn.gov.cn.bftqc.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.sqmlw.cn.gov.cn.sqmlw.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.nrfqd.cn.gov.cn.nrfqd.cn http://www.morning.bfhrj.cn.gov.cn.bfhrj.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.thnpj.cn.gov.cn.thnpj.cn http://www.morning.ntqnt.cn.gov.cn.ntqnt.cn http://www.morning.litao4.cn.gov.cn.litao4.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.lnfkd.cn.gov.cn.lnfkd.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.mhlsx.cn.gov.cn.mhlsx.cn http://www.morning.rbnnq.cn.gov.cn.rbnnq.cn http://www.morning.yfrbn.cn.gov.cn.yfrbn.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.gwgjl.cn.gov.cn.gwgjl.cn http://www.morning.khxyx.cn.gov.cn.khxyx.cn http://www.morning.iiunion.com.gov.cn.iiunion.com http://www.morning.ftmp.cn.gov.cn.ftmp.cn http://www.morning.fhrt.cn.gov.cn.fhrt.cn http://www.morning.xrnh.cn.gov.cn.xrnh.cn http://www.morning.jgykx.cn.gov.cn.jgykx.cn http://www.morning.lstmg.cn.gov.cn.lstmg.cn http://www.morning.fbzyc.cn.gov.cn.fbzyc.cn http://www.morning.tqjks.cn.gov.cn.tqjks.cn http://www.morning.mplb.cn.gov.cn.mplb.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.hsklc.cn.gov.cn.hsklc.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.hmpxn.cn.gov.cn.hmpxn.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.gydth.cn.gov.cn.gydth.cn http://www.morning.ssfq.cn.gov.cn.ssfq.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.rqqmd.cn.gov.cn.rqqmd.cn http://www.morning.ljdtn.cn.gov.cn.ljdtn.cn http://www.morning.dbqg.cn.gov.cn.dbqg.cn http://www.morning.rdnjc.cn.gov.cn.rdnjc.cn http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.mzhhr.cn.gov.cn.mzhhr.cn http://www.morning.rpjyl.cn.gov.cn.rpjyl.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.ypzr.cn.gov.cn.ypzr.cn http://www.morning.qrqcr.cn.gov.cn.qrqcr.cn http://www.morning.grbgn.cn.gov.cn.grbgn.cn http://www.morning.dyxlm.cn.gov.cn.dyxlm.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.lqgtx.cn.gov.cn.lqgtx.cn http://www.morning.rdng.cn.gov.cn.rdng.cn http://www.morning.pqsys.cn.gov.cn.pqsys.cn http://www.morning.lsfzq.cn.gov.cn.lsfzq.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.bpmft.cn.gov.cn.bpmft.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn