合并两个已排序的链表,考到烂得不能再烂的经典题,但是很多人写这段代码会有这样或那样的问题

这里我给出了我的C++算法实现

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1 && !l2) return l1;
if(l2 && !l1) return l2;
if( !l2 && !l1) return NULL; //保证所有列表不为空
ListNode* t1 = l1;
ListNode* t2 = l2;
ListNode* t = NULL;
if(t1->val < t2->val){ //确定表头t是l1还是l2
t = t1;
t1 = t1->next;
}
else{
t = t2;
t2 = t2->next;
}
for(;t1&&t2; t = t->next){//确定表头t的下一个元素
if(t1->val < t2->val){
t->next = t1;
t1 = t1->next;
}
else{
t->next = t2;
t2 = t2->next;
}
}
if(t1){//t1不为空,将l1剩余部分插入到t后
t->next = t1;
}
if(t2){//t2不为空,将l2剩余部分插入到t后
t->next = t2;
}
if(l1->val < l2->val) return l1;
else return l2;//确定表头
}
};

Leetcode 21 Merge Two Sorted Lists 链表的更多相关文章

  1. [LeetCode] 21. Merge Two Sorted Lists 合并有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. [LeetCode] 21. Merge Two Sorted Lists 混合插入有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

  4. (链表) leetcode 21. Merge Two Sorted Lists

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  5. [leetcode]21. Merge Two Sorted Lists合并两个链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  6. LeetCode 21. Merge Two Sorted Lists (合并两个有序链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  7. LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

    题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splici ...

  8. [LeetCode]21. Merge Two Sorted Lists合并两个有序链表

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  9. Java [leetcode 21]Merge Two Sorted Lists

    题目描述: Merge two sorted linked lists and return it as a new list. The new list should be made by spli ...

随机推荐

  1. 爬虫--scrapy--windows上安装

    关于scrapy的安装网上有一大把教程,但是比较麻烦,各种包----------,这里给大家介绍一款神器: 下载地址:http://continuum.io/downloads 根据自己电脑的系统选择 ...

  2. [jQuery]最新的 3.0 已发布

    可以从 https://code.jquery.com/jquery/#jquery-all-1.x 找到历史版本 1.12.4 是支持 IE6 - 8 的最高版本. 下载地址 http://file ...

  3. [刘阳Java]_Java环境搭建_第2讲

    1.为什么搭建Java的环境 Java的程序语言不能独立在操作系统上运行 Java程序需要一个JVM(Java虚拟机)才能将程序员写好的Java程序运行在操作系统 Java程序的跨平台(Mac, Li ...

  4. 将asp.net页面弄成伪静态

    在Web.config中写: <RewriterConfig>     <Rules> <RewriterRule>         <LookFor> ...

  5. Android.技术站点

    总结Android相关的技术站点和blog 1. http://android-developers.blogspot.com/ 首推这个blog,有时间需要每篇blog读一遍. 2. nlopez ...

  6. 验证码(网页的某些图片)在ie 360不显示,在火狐下显示正常

    解决办法: 开始->运行,在运行输入框中输入“regsvr32 c:\windows\system32\pngfilt.dll”(不包含双引号),然后点击确定,如果在出现“已加载c:\windo ...

  7. handlebars

    Handlebars 是 JavaScript 一个语义模板库,通过对view和data的分离来快速构建Web模板.它采用"Logic-less template"(无逻辑模版)的 ...

  8. oracle 安装注意

    1. 本地安装oracle数据库后,并不代表可以用plsql 连接上了.. 如果安装的是64位的oracle,plsql 是不能直接连接的.. 2. 如果是64位的..需要下载一个oracle 客户端 ...

  9. Apache配置默认首页面

    conf -> httpd.conf下设置成 <IfModule dir_module> DirectoryIndex index.php index.html index.htm ...

  10. three.js 之旅 (五)--跟场景scene相关的函数

    1.scene.add(obj);   在场景中添加物体 2.scene.remove(obj);   在场景中移除物体 3.scene.children();  获取场景中所有子对象的列表 4.sc ...