21. Merge Two Sorted Lists (Easy)

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.

Example:

Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4

solution##

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode head = new ListNode(-1);
ListNode p = head, temp = null;
while (l1 != null && l2 != null)
{
if (l1.val > l2.val)
{
temp = l2;
l2 = l2.next;
}
else
{
temp = l1;
l1 = l1.next;
}
p.next = temp;
p = temp;
}
p.next = l1 != null ? l1 : l2;
return head.next;
}
}

总结##

题意是给定两个有序链表l1和l2,将这两个链表合并成一个有序链表。我的思路是构造一个头结点head,然后用一个while循环遍历这两个链表,如果l1.val大于l2.val,则将l2结点用temp变量保存起来,然后l2指向下一个结点,否则,则将l1结点用temp变量保存起来,然后l1指向下一个结点;接着使用尾插法将temp结点插到head链表的尾部;循环结束之后,再将剩下的结点直接插入到head链表尾部,最后返回head.next结点。

还有一种思路是用递归,这里就不细说了。

Notes

1.链表的插入,删除操作最好构造一个额外的头结点,方便操作的统一;

2.此题与两个有序数组的合并是一样的思路;

LeetCode--LinkedList--21.Merge Two Sorted Lists (Easy)的更多相关文章

  1. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

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

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

  3. Leetcode练习题21. Merge Two Sorted Lists

    题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...

  4. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

  5. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

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

  7. 【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 splici ...

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

  9. LeetCode:21. Merge Two Sorted Lists(Easy)

    1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...

随机推荐

  1. Word文档创建目录

    一.以设置两级目录为例: 1.设置两个标题,标题1对应第一级目录,标题2对应第二级目录. 点击标题1,点击修改: 设置好样式和格式: 同理设置标题2. 2.创建多级目录: 选择级别1,关联到标题1,设 ...

  2. numpy basic sheatsheet

    NumPy(Numerical Python) 是 Python 语言的一个扩展程序库,支持大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库.NumPy 通常与 SciPy(Scien ...

  3. Nightmare BFS

    Ignatius had a nightmare last night. He found himself in a labyrinth with a time bomb on him. The la ...

  4. S7通信协议之你不知道的事儿

    在电气学习的路上,西门子PLC应该是我的启蒙PLC,从早期的S7-300/400 PLC搭建Profibus-DP网络开始接触,到后来的S7-200Smart PLC,再到现在的S7-1200/150 ...

  5. api测试用例(编写思路)

    在API的自动化测试维度中,测试维度分为两个维度,一个是单独的对API的验证,客户端发送一个请求后,服务端得到客户端的请求并且响应回复给客户端: 另外一个维度是基于业务场景的测试,基于业务场景的也就是 ...

  6. 文字检测模型EAST应用详解 ckpt pb的tf加载,opencv加载

    参考链接:https://github.com/argman/EAST (项目来源) https://github.com/opencv/opencv/issues/12491  (遇到的问题)    ...

  7. 想学习CTF的一定要看这篇,让你学习效率提升80%

    在学习CTF过程中你是否遇到这样的情况: 下定决心想要学习CTF,不知道从哪里开始? 找了一堆CTF相关的知识学习,但是知识点太凌乱,没有统一明确的学习路径. 又或者理论学习完,没有相应的实操环境? ...

  8. ubuntu17.10安装lnmp安装包的核心问题-gcc版本、g++版本

    大致碰到的问题都是这样,不是php安装失败,就是MySQL安装失败,或者Nginx也安装失败 基本上是花式报错.后来在军哥的论坛中找到了这个帖子:https://bbs.vpser.net/viewt ...

  9. dockerfile简介及书写规则

                                       Dockerfile 简介 Dockfile是一种被Docker程序解释的脚本, Dockerfile由一条一条的指令组成,每条指 ...

  10. 【小技巧】【App store切换为中文】

    为什么80%的码农都做不了架构师?>>>   贡献作者 -[XJDomain]博客XJ:  https://my.oschina.net/shengbingli/blogGitHub ...