LeetCode OJ--Merge Two Sorted Lists
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的更多相关文章
- Leetcode OJ : Merge k Sorted Lists 归并排序+最小堆 mergesort heap C++ solution
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode ...
- 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. 解 ...
- [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 ...
- [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 ...
- 蜗牛慢慢爬 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 ...
- LeetCode 23 Merge k Sorted Lists(合并k个有序链表)
题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...
- [Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/merge-two-sorted-lists/descrip ...
- [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 ...
- 【leetcode】Merge k Sorted Lists
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- 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 ...
随机推荐
- 阿里大鱼短信发送 FOR DT
//增加了参数$action 来标志发送的是什么短信 注册短信 验证码短信 提示短信等 function send_sms($mobile, $message, $word = 0, $time = ...
- day14-推导式和生成器表达式
1.推导式规则 [每一个元素或者是和元素相关的操作 for 元素 in 可迭代数据类型] ----------遍历之后挨个处理[满足条件的元素相关的操作 for 元素 in 可迭代数据类型 if 元素 ...
- Python PyAudio 安装使用
Python PyAudio安装: Python3.7 无法安装pyaudio pip install pyaudio提示error: Microsoft Visual C++ 14.0 is req ...
- jflash合并两个文件
有时候需要将两个代码块烧写进入单片机的flash,可以使用合并的方法将两个文件合并为一个文件进行烧写,也可以分两次烧写,但要注意不要擦写不相关的存储空间. 打开J-FLASH,新建一个工程,然后fil ...
- CSS(非布局样式)
CSS(非布局样式) 问题1.CSS样式(选择器)的优先级 1.计算权重 2.!important 3.内联样式比外嵌样式高 4.后写的优先级高 问题2.雪碧图的作用 1.减少 HTTP 请求数,提高 ...
- HDU 4738 双连通分量 Caocao's Bridges
求权值最小的桥,考虑几种特殊情况: 图本身不连通,那么就不用派人去了 图的边双连通分量只有一个,答案是-1 桥的最小权值是0,但是也要派一个人过去 #include <iostream> ...
- 20188472 https://www.cnblogs.com/chenzg90826/
我是一名学计算机的大一学生,对学计算机比较感兴趣,但是对于计算机的了解程度还不够深,所以我在这方面还只是一个初学者.经过了一个学期对计算机和编程语言的学习,我觉得要真正的学好这门专业真的还要更努力.在 ...
- Nginx与Lua的开发
1. Lua基础语法 安装lua hello world 也可以编写lua脚本 运行脚本 lua注释 变量 局部变量的话前面加个local 循环 if语句 2. Nginx与Lua开发环境 https ...
- MongoDB学习-->Spring Data Mongodb框架之Repository
application-dev.yml server: port: 8888 mongo: host: localhost port: 27017 timeout: 60000 db: mamabik ...
- 蓝桥杯Java输入输出相关
转载自:http://blog.csdn.net/Chen_Tongsheng/article/details/53354169 一.注意点 1. 类名称必须采用public class Main方式 ...