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. 【个人训练】(UVa146)ID Codes

    题意与解析 这题其实特别简单,求给定排列的后继.使用stl(next_permutation)可以方便地解决这个问题.但是,想要自己动手解就是另外一回事了.我的解法是从后往前找到第一个$a_i$比$a ...

  2. 自动化测试学习之路--java 数组

    数组的定义与为数组元素分配空间和赋值是分开进行的,称为动态初始化. 在数组定义的同时就为数组元素分配空间并赋值,称为静态初始化. 一维数组举例: //动态初始化 int[] intArr; intAr ...

  3. QC的使用学习(二)

    今日学习清单: 1.Quality  Center中左上角选项中(QC 10.0中文版)工具菜单下的自定义中的几个内容,有:用户属性.组.项目用户.模块访问.需求类型.项目列表等.用户属性打开后是对当 ...

  4. python接口测试(三)——Excell文件读取进行参数化

    python进行http请求时,需要对参数进行参数化,此时就可以运用Excel进行,具体如下: 1.梳理出请求中那些参数需要参数化,然后新建一个Excel,如图: 2.读取Excel中的内容,在读取前 ...

  5. Struts2(四.注册时检查用户名是否存在及Action获取数据的三种方式)

    一.功能 1.用户注册页面 <%@ page language="java" contentType="text/html; charset=UTF-8" ...

  6. Spring实战第四章学习笔记————面向切面的Spring

    Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...

  7. packstack安装ironic

    KVM Centos7.3虚机 安装openstack Pike版本, 其它版本安装方法类似. packstack目前对NetworkManager 还不支持,我们修改下配置: systemctl d ...

  8. Spark概念介绍

    Spark概念介绍:spark应用程序在集群中以一系列独立的线程运行,通过驱动器程序(Driver Program)发起一系列的并行操作.SparkContext对象作为中间的连接对象,通过Spark ...

  9. MyEclipse主题设置

    1. 打开网页: http://eclipsecolorthemes.org/ 选择自己喜欢的主题,并下载(下载epf文件) 我下载的是 Vibrant Ink 2. 下载完成后,打开myeclips ...

  10. web开发速查表(php,css,html5........)