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

这里我给出了我的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. 最长子串 FZU2118

    http://acm.fzu.edu.cn/problem.php?pid=2128 分析:利用strstr()函数将每个字串在原串中的首尾位置存储一下,再将首尾从小到大排一下序.(写着写着就感觉和看 ...

  2. H5-表格、表单

    一.表格 1.表格标签 a.table 表格 b.thead 表格头 c.tbody 表格主体 d.tr 表格行 e.th 元素定义表头 f.td 元素定义表格单元 2.表格样式重置 a.table{ ...

  3. Oracle rowid

    本文讨论的是关于oracle从8i开始引进object的概念后的rowid,即扩展(extended)的rowid:1.rowid的介绍先对rowid有个感官认识:SQL> select ROW ...

  4. quick-cocos2d-x之testlua之mainMenu.lua

    require "helper" require "testResource" require "ActionsTest.ActionsTest&qu ...

  5. 阿牛OCX编程助手

    ※◆☆★☆◆※欢迎使用阿牛OCX编程助手,此程序为按键精灵专用,如终请联系作者QQ:82850696*0*测试版已停用*0*2014-12-27 14:05:59*哈密*E2873D0137C6D04 ...

  6. js 判断字符为空

    function checkIsNull(value){ if(typeof value=='undefined'){ return true; } if(value==null){ return t ...

  7. jsp-avaBean

    package javaBean; public class pagecount { private long count=0; public long getcount() { return cou ...

  8. linux ps命令

    名称:ps 使用权限:所有使用者 使用方式:ps [options] [--help] 说明:显示瞬间行程 (process) 的动态 参数: ps 的参数非常多, 在此仅列出几个常用的参数并大略介绍 ...

  9. 贪心+构造( Codeforces Round #344 (Div. 2))

    题目:Report 题意:有两种操作: 1)t = 1,前r个数字按升序排列:   2)t = 2,前r个数字按降序排列: 求执行m次操作后的排列顺序. #include <iostream&g ...

  10. 修改Broforce无限人数,死亡不减反加

    看B站直播发现这个有趣的游戏,找了半天修改器无效,Cheat Engine怎么找指针有点忘了,直接找数值每关都要重来,想来想去还是简单粗暴的反编译好了. 顺便做下C#反编译备忘. 首先把DLL反成IL ...