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的更多相关文章

  1. 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. 解 ...

  2. [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 ...

  3. 蜗牛慢慢爬 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 ...

  4. LeetCode 23 Merge k Sorted Lists(合并k个有序链表)

    题目链接: https://leetcode.com/problems/merge-k-sorted-lists/?tab=Description Problem: 给出k个有序的list, 将其进行 ...

  5. [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 ...

  6. [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 ...

  7. 【leetcode】Merge k Sorted Lists

    Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...

  8. 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 ...

  9. [leetcode] 21. Merge Two Sorted Lists (Easy)

    合并链表 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class ...

随机推荐

  1. Windows模拟linux终端工具Cmder+Gow

    1. 说明 Cmder:Windows下的终端模拟器. Gow: Windows下模拟Linux命令行工具集合.可以在windows执行linux下的大部分命令,如ls.grep.xargs等. 2. ...

  2. 问题 C: 质因数的个数

    1947: 质因数的个数 时间限制: 1 Sec  内存限制: 32 MB提交: 245  解决: 114[提交][状态][讨论版][命题人:外部导入] 题目描述 求正整数N(N>1)的质因数的 ...

  3. Visual Studio 2003安装包

    点击下载

  4. LeetCode 29——两数相除

    1. 题目 2. 解答 2.1. 方法一 题目要求不能使用乘法.除法和除余运算,但我们可以将除法转移到对数域. \[ \frac{a}{b} = e^{\frac{lna}{lnb}} = e^{ln ...

  5. CCS Font 知识整理总结

    总是搞不懂 CCS 中如何正确的使用字体,这下明白了. 1.什么是 font-face font-face 顾名思义,就是文字的脸.字体是文字的外在形式,就是文字的风格,是文字的外衣.比如行书.楷书. ...

  6. python 调用RESTFul接口

    本周需要将爬虫爬下来的数据入库,因为之前已经写好PHP的接口的,可以直接通过python调用PHP接口来实现,所以把方法总结一下. //python编码问题,因为好久用,所以很容易出现 # -*- c ...

  7. C#编译和运行原理

    关于编译与内存的关系,以及执行时内存的划分 1.所谓在编译期间分配空间指的是静态分配空间(相对于用new动态申请空间),如全局变量或静态变量(包括一些复杂类型的 常量),它们所需要的空间大小可以明确计 ...

  8. 3GPP规范命名规则解读

    http://blog.sina.com.cn/s/blog_6b10255301012co6.html 学习了解电信技术知识的一个很好的手段是阅读3GPP的规范.但是3GPP有大量的规范,我们可能经 ...

  9. [剑指Offer] 7.斐波那契数列

    class Solution { public: int Fibonacci(int n) { ] = {}; res[] = ; res[] = ; ;i < n;i ++){ res[i] ...

  10. [BZOJ4212]神牛的养成计划

    [BZOJ4212]神牛的养成计划 试题描述 Hzwer 成功培育出神牛细胞,可最终培育出的生物体却让他大失所望...... 后来,他从某同校女神 牛处知道,原来他培育的细胞发生了基因突变,原先决定神 ...