http://oj.leetcode.com/problems/merge-two-sorted-lists/

有序链表的归并排序

#include <iostream>
using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; class Solution {
public:
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// default as asend sorted,merge sort
ListNode *ans = NULL,*ansEnd = NULL,*n1 = l1,*n2 = l2;
bool firstone = ;
while(n1&&n2)
{
while(n1->val <= n2 ->val)
{
if(firstone == )
{
ans = ansEnd = n1;
//ansEnd = ans->next;
firstone = ;
n1 = n1->next;
}
else
{
ansEnd->next = n1;
ansEnd = n1;
n1 = n1->next;
}
if(n1 == NULL)
break;
}
if(n1 == NULL)
break;
while(n1->val > n2->val)
{
if(firstone == )
{
ans = ansEnd = n2;
firstone = ;
n2 = n2->next;
}
else
{
ansEnd->next = n2;
ansEnd = n2;
n2 = n2->next;
}
if(n2 == NULL)
break;
}
}
if(n1==NULL && n2!= NULL)
{
if(firstone ==)
{
ans = n2;
return ans;
}
ansEnd->next = n2;
}
else if(n2 == NULL && n1 != NULL)
{
if(firstone == )
{
ans = n1;
return ans;
}
ansEnd->next = n1;
}
return ans;
}
}; int main()
{
ListNode *n1 = new ListNode();
ListNode *n2 = new ListNode();
ListNode *n3 = new ListNode(); n1->next = n2; Solution myS;
ListNode *ans = myS.mergeTwoLists(NULL,NULL);
return ;
}

LeetCode OJ--Merge Two Sorted Lists的更多相关文章

  1. Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution

    /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...

  2. Java for LeetCode 023 Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解 ...

  3. [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 ...

  4. [LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. E ...

  5. 蜗牛慢慢爬 LeetCode 23. Merge k Sorted Lists [Difficulty: Hard]

    题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...

  6. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  7. [Leetcode Week4]Merge Two Sorted Lists

    Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...

  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. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  10. Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

    Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list shoul ...

随机推荐

  1. zabbix3.2安装手册

    Alexei Vladishev创建了Zabbix项目,当前处于活跃开发状态,Zabbix SIA提供支持. Zabbix是一个企业级的.开源的.分布式的监控套件 Zabbix可以监控网络和服务的监控 ...

  2. Inkscape基础

    What is Inkscape A program for creating vector graphics For Windows, Mac OS, and Linux Open source F ...

  3. classpath、WEB-INF

    classpath是指 WEB-INF文件夹下的classes目录(war包),对于springboot项目打包出来的jar包,里面的就是BOOT-INF: 这个demo的源码结构如下: 可见,jav ...

  4. asp发送短信验证码 pst方式

    <script language="jscript" runat="server">  Array.prototype.get = function ...

  5. nginx静态资源web服务

    静态资源:非服务器动态运行生成的文件 浏览器端渲染:html ,css,js 图片:jpeg,gif,png 视频:flv ,mpeg 文件:txt,等任意下载文件 静态资源服务场景:CDN 文件读取 ...

  6. QT入门学习笔记1:为什么要选QT及QT软件下载

    为什么选择QT? Qt突出的优势: ◆ Qt 是基于 C++ 的一种语言扩展(Extention) C/C++ 目前还是一种很多人都在学习的语言. Qt的好处就在于Qt本身可以被称作是一种 C++ 的 ...

  7. leetcode-14-basic-breadthFirstSearch

    BFS: breadth first search 107. Binary Tree Level Order Traversal II 解题思路: 本来我是用map<int,int>存所有 ...

  8. poj-1700 crossing river(贪心题)

    题目描述: A group of N people wishes to go across a river with only one boat, which can at most carry tw ...

  9. UVa - 12096 集合栈计算机(STL)

    [题意] 有一个专门为了集合运算而设计的“集合栈”计算机.该机器有一个初始为空的栈,并且支持以下操作:PUSH:空集“{}”入栈DUP:把当前栈顶元素复制一份后再入栈UNION:出栈两个集合,然后把两 ...

  10. Servlet注意事项

    注意事项 1.对于Servlet的应用程序的目录结构来说,若想让有些文件Servlet可以访问,而用户不能访问的时候,可以将其放置在WEB-INF目录下 2.ServletResponse中getwr ...