[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 ...
随机推荐
- 国际电话区号SQL
CREATE TABLE `phone_prefix` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `country` varchar(30) N ...
- weblogic中配置自定义filter和servlet
情景:最近公司产品要接入其它厂商的单点服务器,本来我是在Tomcat上进行测试,使用的是spring boot 的注解方式@webFilter和@webServlet注解写过滤器和servlet类,启 ...
- 阿里的100TB Sort Benchmark排序比雅虎快了一倍还多,我的看法
如果我的判断正确,它们使用的软件和算法应该是HADOOP,MAP/REDUCE,或者类似的技术方案.如果这些条件一样,影响计算结果的还有三个因素: 1.CPU的数量和CPU的处理能力 CPU的 ...
- CCS Font 知识整理总结
总是搞不懂 CCS 中如何正确的使用字体,这下明白了. 1.什么是 font-face font-face 顾名思义,就是文字的脸.字体是文字的外在形式,就是文字的风格,是文字的外衣.比如行书.楷书. ...
- 学习bash——通配符与特殊符号
一.通配符 这是bash操作环境中一个非常有用的功能,这让我们使用bash处理数据就更方便了. 常用通配符如下: 符号 意义 * 代表0个到无穷多个任意字符 ? 代表一个任意字符 [] 代表一定有一个 ...
- uva 116 Unidirectional TSP(动态规划,多段图上的最短路)
这道题目并不是很难理解,题目大意就是求从第一列到最后一列的一个字典序最小的最短路,要求不仅输出最短路长度,还要输出字典序最小的路径. 这道题可以利用动态规划求解.状态定义为: cost[i][j] = ...
- WebKit 渲染过程
webkit笔记,主要来自 朱永盛 <WebKit技术内幕> 学习笔记,转载就注明原著,该书是国内仅有的Webkit内核的书籍,学习的好导师,推荐有兴趣的朋友可以购买 Webkit渲染过程 ...
- shit element ui & form password validation
shit element ui & form password validation shit docs https://github.com/yiminghe/async-validator ...
- [剑指Offer] 31.整数中1出现的次数
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- java高精度类尝试
java高精度尝试, poj2109,比较坑的题目 import java.io.*; import java.util.*; import java.math.*; public class Mai ...