【LeetCode OJ】Merge Two Sorted Lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
{
if (l2 == NULL)
return l1;
if (l1 == NULL)
return l2;
ListNode *r, *head;
head = NULL;
if (l1->val <= l2->val) //哪个链表的第一个节点小就把头指针指向哪个节点
{
head = l1;
l1 = l1->next;
}
else
{
head = l2;
l2 = l2->next;
}
r = head;
while (l1&&l2) //比较大小
{
if (l1->val <= l2->val)
{
r->next = l1;
l1 = l1->next;
}
else
{
r->next = l2;
l2 = l2->next;
}
r = r->next;
}
if (l1 == NULL) //l1为空,直接连接l2
{
r->next = l2;
}
if (l2 == NULL) //l2为空,直接连接l1
{
r->next = l1;
}
return head;
}
【LeetCode OJ】Merge Two Sorted Lists的更多相关文章
- 【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】【数组归并】Merge k Sorted Lists
描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity ...
- LeetCode OJ:Merge k Sorted Lists(归并k个链表)
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类 ...
- LeetCode OJ: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][Python]21: Merge Two Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...
- [Leetcode][Python]23: Merge k Sorted Lists
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 23: Merge k Sorted Listshttps://oj.leet ...
- LeetCode LinkList 23. Merge k Sorted Lists
这两天一直也没有顾上记录一下自己做过的题目,回头看看,感觉忘的好快,今天做了一个hard,刚开始觉得挺难得,想了两种方法,一种是每次都从k个list中选取最小的一个,为空的直接跳过,再就是每次合并其中 ...
- C# 写 LeetCode easy #21 Merge Two Sorted Lists
21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...
随机推荐
- 查看eclipse版本信息
http://www.cnblogs.com/caiyuanzai/archive/2013/01/11/2855796.html 如果要查询eclipse数字版本号的话,可按如下进行操作: 1. 找 ...
- C# Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 80070005
环境说明: Win2008 R2(中文版) x64 .IIS 7.0 功能说明:上传Excel到服务器,并在服务器端读取Excel中的数据: 异常信息:Retrieving the COM class ...
- Linux可视化服务器管理工具webmin
webmin是一个可视化的linux服务器管理工具,可以帮助我们实现很多功能. Webmin官网: http://www.webmin.com/ 下载地址:http://prdownloads.sou ...
- 创建 React-Native 工程时,如何指定特定的 React-Native 版本
react-native 可能会出现一种情况,就是版本最高的可能出现有些东西不太稳定,这时候要用到旧的版本怎么办?就可以用以下方法创建项目. 0. 原因 创建新的 React-Native (以下简称 ...
- JSP内置对象—session
什么是session? session对象是用来在每个用户之间分别保存每个用户信息的对象,以便跟踪用户的操作状态.session的信息保存在server端,session的id保存在client的co ...
- The difference between the request time and the current time is too large.阿里云oss上传图片报错
The difference between the request time and the current time is too large. 阿里云oss上传图片的时候报错如上, 解决办法,把 ...
- Oracle12c 在 Ubuntu 12.04 ~ 18.04 的安装注意事项
必须的注意点: 1:/bin/sh 必须指向 bash or ksh 2:/usr/lib64 可以忽略的事情: 1:gcc 版本无所谓 2:libstdc++5 无需安装 3:libaio 版本无所 ...
- redis 的set数据类型
相关命令 1.SADD SADD key-name item1 [item 2…] 将一个或多个成员元素加入到集合中 2.SREM SMEMBERS key-name item1 [item 2…] ...
- Nine-patch
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch This NinePatch defi ...
- 破解IT运维成本困境,专业化分工是妙方
随着IT建设的不断深入和发展,IT运维成为了企业运营的必需品.许多企业的IT预算相比于去年虽然有了很大的提高,但总体来说还是非常紧张.上周,我参加了一个CIO沙龙研讨会,现场调查问到目前CIO在IT运 ...