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. 亲测可以使用的Axmath和MathPix插入word公式

    Axmath破解版链接 链接:https://pan.baidu.com/s/1Phak8mc3msKAMQ6H_5EN5g 提取码:glti MathPixTool和Axmath共同使用向word插 ...

  2. Daily Scrum 1/14/2016

    Zhaoyang & Yandong: Still optimizing the speech input interface Dong & Fuchen: Image asynchr ...

  3. Cyclic Nacklace 杭电3746

    CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, ...

  4. Gatling 条件判断

    在使用Gatling的过程中,当前置接口异常,无法获取到数据作为其他接口的请求参数室,接口是不能请求的.或者通过feeder获取的数据要区分不同的情况请求不同的接口.此时,使用gatling的判断语句 ...

  5. JavaScript基础1228JavaScript:void(0)开始----

    JavaScript:void(0)含义 JavaScript:void(0)含义 我们经常会使用到JavaScript:void(0)这样的代码,那么在JavaScript中JavaScript:v ...

  6. PHP函数:memory_get_usage

    memory_get_usage()  -返回分配给 PHP 的内存量 说明: memory_get_usage ([ bool $real_usage = false ] ) : int 参数: r ...

  7. python 基础篇 自定义函数

    多态 我们可以看到,Python 不用考虑输入的数据类型,而是将其交给具体的代码去判断执行,同样的一个函数(比如这边的相加函数 my_sum()),可以同时应用在整型.列表.字符串等等的操作中. 在编 ...

  8. git在用https进行push时候免输账密的方法

    先新建一个文件 $ touch ~/.git-credentials $ vim ~/.git-credentials 进去添加内容(github为github.com,码云为gitee.com) h ...

  9. 关于Pandownload和百度网盘

    本周,百度网盘第三方客户端 Pandownload 被查,开发者被“跨省追捕”:百度网盘“用户激励计划”在未充分告知用户的情况下,利用用户自己的电脑做 P2P 上传节点.这两件事再度引发了对百度网盘的 ...

  10. 【三剑客】awk运算符

    1. 算术运算符:+,-,*,/,% [root@oldboy test]# awk 'BEGIN{a=50;b=20;print "(a+b)=",(a+b)}' (a+b)= ...