[Leetcode Week4]Merge Two Sorted Lists
Merge Two Sorted Lists题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/merge-two-sorted-lists/description/
Description
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.
Solution
/**
* 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) {
ListNode *n1, *n2, *preNode;
ListNode *head = new ListNode(0);
preNode = head;
n1 = l1, n2 = l2;
while (true) {
if (!n1 && !n2) {
break;
} else if (!n1 && n2) {
preNode -> next = new ListNode(n2 -> val);
n2 = n2 -> next;
} else if (n1 && !n2) {
preNode -> next = new ListNode(n1 -> val);
n1 = n1 -> next;
} else {
if (n1 -> val < n2 -> val) {
preNode -> next = new ListNode(n1 -> val);
n1 = n1 -> next;
} else {
preNode -> next = new ListNode(n2 -> val);
n2 = n2 -> next;
}
}
preNode = preNode -> next;
}
return head -> next;
}
};
解题描述
这道题还是算水题一道,只是单纯想用来复习下链表操作(第一次运行就段错误)。主要的思路也就是在两个链表中分别设置游标,比较游标位置上的数值然后将较小的数值插入到新的链表中。
[Leetcode Week4]Merge Two Sorted Lists的更多相关文章
- 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 [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] 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】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 ...
- [leetcode] 21. Merge Two Sorted Lists (Easy)
合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...
随机推荐
- C++学习009预处理器指令符号 # ## #@ 符号的使用
# ## #@ 符号是预处理器指令符号. 当预处理器遇到#指令符号时,会将#之后的部分用双引号括起来 当预处理去遇到##指令符号时,直接将##前后部分连接起来 当预处理器遇到#@指令符号,将#@之后的 ...
- 怎样安装PyCharm
在地址栏输入http://www.jetbrains.com/pycharm/ 打开PyCharm官网 http://idea.lanyus.com/
- python 基础篇 06 编码 以及小知识点补充
本节主要内容: 1. is和==的区别2. 编码的问题 ⼀. is和==的区别1. id()通过id()我们可以查看到⼀个变量表⽰的值在内存中的地址 注 ----<<<在pytho ...
- KVM WEB管理工具——WebVirtMgr(二)日常配置
配置宿主机 1.登录WebVirtMgr管理平台 2.添加宿主机 选择首页的WebVirtMgr -->Addd Connection 选择“SSH链接“,设置Label,IP,用户 注意:La ...
- Queue模块初识
Queue模块实现了多生产者.多消费者队列.它特别适用于信息必须在多个线程间安全地交换的多线程程序中.这个模块中的Queue类实现了所有必须的锁语义.它依赖于Python中线程支持的可用性:参见thr ...
- kaldi解码及特征提取详解
目录 1. 注意事项 2. 流程图: 3. 具体流程指令: 1. 注意事项 首先要训练好模型,用到3个文件,分别是: final.mdl(训练模型得到的模型文件) final.mat(用来特征转换) ...
- MAC下Python3.5安装问题
mac中自动集成了python2.7,但是作为程序员总是希望用最新的版本, 刚才安装python3.5后,python -V,依然提示是,2.7: 然后在 .bash_profile后面找到pytho ...
- CentOS7 最小化安装vmware-tools
花了一上午的时间在1611上安装vmware-tool,总不能全部顺利安装成功 结合网上资料,整理出正确流程 下载最新的CentOS-7-x86_64-Minimal-1708 安装之后 联网 yum ...
- jQuery - AJAX get()和post()方法
jQuery get()和post()方法用于通过HTTP GET或POST请求从服务器请求数据. HTTP请求:GET VS POST 两种在客户端和服务器端进行请求-响应的常用方法是:GET和PO ...
- 使用HTML5的JavaScript选择器操作页面中的元素
<!doctype html><html lang="en"> <head> <meta charset="UTF-8& ...