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 ...
随机推荐
- 【卡常 bitset 分块】loj#6499. 「雅礼集训 2018 Day2」颜色
好不容易算着块大小,裸的分块才能过随机极限数据:然而这题在线的数据都竟然是构造的…… 题目描述 有 $n$ 个数字,第 $i$ 个数字为 $a_i$. 有 $m$ 次询问,每次给出 $k_i$ 个区间 ...
- python入门:输出1-10的所有数(自写)
#!/usr/bin/env python # -*- coding:utf-8 -*- #输出1-10的所有数(自写) """ 导入time库,给kaishi赋值为数字 ...
- 第6章 AOP与全局异常处理6.5-6.11 慕课网微信小程序开发学习笔记
https://coding.imooc.com/learn/list/97.html 目录: 第6章 AOP与全局异常处理6-1 正确理解异常处理流程 13:236-2 固有的处理异常的思维模式与流 ...
- python 类的使用
目录 类的继承 类的派生 类的组合 菱形继承问题 多态与多态性 dataclass的使用 类的继承 什么是继承,在生活中,子承父业,父亲和儿子就是继承的关系 在python中,父类和子类(派生类),父 ...
- Python入门学习笔记2:刷题
1) LeetCode 强的面试题和算法题,要求也比较高,很多国内外的码农在上面刷题.难度从easy到hard都有,而且覆盖面极广,需要你的综合实力去答题. 最简单的题比如字符串的处理有的时候也要用到 ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- LeetCode(172)Factorial Trailing Zeroes
题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- centos7 安装显卡驱动方法
方法一: 首先需要添加一个第三方的源ELRepo.这个源支持RED HAT系的Linux系统,主要是提供一些硬件的驱动程序.这个源的主页如下: http://elrepo.org/tiki/tiki- ...
- 欧拉函数:HDU1787-GCD Again(欧拉函数的模板)
GCD Again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- window 7上安装Visual Studio 2017失败的解决方法
今天在办公电脑上windows 7系统上装Visual Studio 2017企业版的时候遇到了一个让人懵逼的错误. 为啥说懵逼呢,因为昨天楼主在台式机上同样安装2017没有任何问题啊,台式机上是wi ...