首页 > 其他分享 >数据结构-->单链表OJ题--->讲解_04

数据结构-->单链表OJ题--->讲解_04

时间:2023-03-27 18:36:11浏览次数:37  
标签:cur2 OJ 04 cur1 -- next 链表 tail SLTNode

本期我们讲解一道 :

两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表所有结点组成的。

现附上图示样例 :

数据结构-->单链表OJ题--->讲解_04_合并双链表

现在上手代码 :>

SLTNode* CombineLists(SLTNode* list1,  ,SLTNode* list2)
{
	if(list1 == NULL)
    	return list2;
    if(list2 == NULL)
        return list1;
    SLTNode* cur1 = list1, cur2 = list2;
    SLTNode* head = NULL, *tail = NULL;
    
    while(cur1 && cur2)
    {
    		if(cur1 ->data < cur2 ->data)
            {
            	if(head == NULL)
                {
                	head = tail = cur1 ->data;
                }
                else
                {
                	tail ->next = cu1 ->data;
                    tail = tail ->next;
                }
                cur1 = cu1 ->next;
            }
        	
        	else
            {
            	if(head == NULL)
                {
                	head = tail = cur2 ->data;
                }
                else
                {
                	tail ->next = cur2 ->data;
                    tail = tail ->next;
                }
                cur2 = cur2 ->next;
            }
    }
    
    // 将剩余链表(过长的那个链表)直接链接在尾部
    if(cu1)
    {
    	tail ->next = cur1;
    }
    
    if(cur2)
    {
    	tail ->next = cur2;
    }
   
    return head;	
}

以上情况也适用于 :>

1.两个链表的长短不一!!

2.以及空链表的特殊情况!!

3.有一个为空,有一个有数据结点!! 

现在,对代码优化处理,设置哨兵位进行简化逻辑!!

代码如下 :>

SLTNode* CombineLists(SLTNode* list1,  ,SLTNode* list2)
{
    SLTNode* cur1 = list1, cur2 = list2;
    SLTNode* guard = NULL, *tail = NULL;
    
    STNode* guard = (STNode*) malloc(sizeof(STNode);
    STNode* tail = (STNode*) malloc(sizeof(STNode);
    tail ->next = NULL;		//防止尾指针乱窜
                                    
    while(cur1 && cur2)
    {
    		if(cur1 ->data < cur2 ->data)
            {                           
                	tail ->next = cu1 ->data;
                   	tail = tail ->next;                
                	cur1 = cu1 ->next;
            }
        	
        	else
            {
                	tail ->next = cur2 ->data;
                    tail = tail ->next;
                	cur2 = cur2 ->next;
            }
    }
                                    
    // 将剩余链表(过长的那个链表)直接链接在尾部    
    if(cu1)
    {
    	tail ->next = cur1;
    }
    
    if(cur2)
    {
    	tail ->next = cur2;
    }
   
	free(guard);
	STNode* head = guard ->next;                                                                       
    return head;	
}

其实,优化后的链表最大的优点是 :> 不再需要对头部结点进行判空处理!!

至此,双链表合并完成!!


标签:cur2,OJ,04,cur1,--,next,链表,tail,SLTNode
From: https://blog.51cto.com/u_15808800/6152836

相关文章

  • iOS7应用开发3、Objective-C
     【跟随教授的讲解和演示,并重做了课上的demo之后,惊奇地发现自己写的程序有bug,界面上12张卡牌出现后,点击任何一个,其他所有卡牌都会变成一块白板……在经历了长时间的调试......
  • iOS7应用开发5、视图控制器View Controller及其生命周期
    1、UITextView:该类与Label类类似,可显示多行,可以编辑内容,可以滚动查看内容;包含属性NSTextStorage*textStorage,该类是NSMutableAttributedString的基类;修改该属性可以自动更......
  • 流量分析:陇剑杯webshell
    参考:https://blog.csdn.net/lkbzhj/article/details/126343675 题目描述:单位网站被黑客挂马,请您从流量中分析出webshell,进行回答: 1、黑客登录系统使用的密码是过......
  • Ubuntu开启root远程登录
    1.使用普通用户登录后切换rootsudo-i2.修改root的密码echoroot:123123|sudochpasswdroot3.如果没有开启sshd就需要安装open-ssh并开启sudoaptupdatesudoap......
  • iOS7应用开发4、Foundation框架
    1、动态绑定:id类型的对象,表示指向未知类型对象的指针;指向对象的实际类型在运行时指定。在使用时,注意check该对象是否响应调用的方法(respondsToSelector)。可以将一个静态类型......
  • iOS7应用开发6:UINavigation, UITabbar控制器的多态性
    1、前期所实现的PlayingCard游戏,其ViewController只能适应PlayingCard这一种游戏规则。而将createDeck函数修改为返回一个nil后,整个ViewController与PlayingCard就没有关......
  • Javascript绝句欣赏
     1.取整同时转成数值型:’10.567890′|0//结果:10’10.567890′^0//结果:10-2.23456789|0//结果:-2~~-2.23456789//结果:-2 2.日期转数值:vard=+ne......
  • sys.exit(),os._exit(),os.kill()
    sys.exit()是退出当前线程,os._exit()是直接退出程序(相当于程序被kill-9直接杀死),os.kill()用于发signal信号1、sys.exit()执行该语句会直接退出程序,这也是经常使用的方法......
  • Swift快速参考手册
       小结了Swift中常用的一些语法供大家参考,主要包括:类的定义方法对象的创建与使用定义变量控制流字符串String例子数组Array例子字典Dictionary例子  ......
  • 八、模块
    pyhton中模块有3中层次类型1.大模块:包(Package)大型的程序通常博阿寒多个文件,按功能相近的原则将文件分组,每个组就是包。包是一种python应用程序执行环境,通常有诺干个中......