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.

解法一:

 /**
* 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) {
if (l1 == NULL || l2 == NULL) {
return l1 ? l1 : l2;
} ListNode * dummy = new ListNode(INT_MIN);
ListNode * temp = dummy; while (l1 && l2) {
if (l1->val > l2->val) {
dummy->next = l2;
l2 = l2->next;
}
else {
dummy->next = l1;
l1 = l1->next;
} dummy = dummy->next;
} if (l1 || l2) {
dummy->next = l1 ? l1 : l2;
} return temp->next;
}
};

由于最后是弄到list1中,但是我们不知道list1还是list2的第一个元素关系,最后结果的list1中的头结点可能会改变,所以需要引入dummy节点。

解法二:

 public ListNode mergeTwoLists(ListNode l1, ListNode l2){
if(l1 == null) return l2;
if(l2 == null) return l1;
if(l1.val < l2.val){
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else{
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}

参考了@yangliguang 的代码

解法三:

 public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1; ListNode handler;
if(l1.val < l2.val) {
handler = l1;
handler.next = mergeTwoLists(l1.next, l2);
} else {
handler = l2;
handler.next = mergeTwoLists(l1, l2.next);
} return handler;
}
}

参考了@RunRunCode 的代码

解法二和解法三都是递归,还没有完全弄明白……

21. Merge Two Sorted Lists【easy】的更多相关文章

  1. Leetcode 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 t ...

  2. # 蜗牛慢慢爬 LeetCode 21. Merge Two Sorted Lists [Difficulty: Easy]

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  3. 两个升序链表的合并 Merge Two Sorted Lists 【 leetcode】

    class Solution {public:   ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {   ListNode *p; ListN ...

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

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

  5. 88. Merge Sorted Array【easy】

    88. Merge Sorted Array[easy] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 ...

  6. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

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

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

  8. 21. Merge Two Sorted Lists(合并2个有序链表)

    21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list s ...

  9. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

随机推荐

  1. luogu P1824 进击的奶牛

    题目描述 Farmer John建造了一个有N(2<=N<=100,000)个隔间的牛棚,这些隔间分布在一条直线上,坐标是x1,...,xN (0<=xi<=1,000,000 ...

  2. [BZOJ 4060] Word Equations

    Link: BZOJ 4060 传送门 Solution: 可以发现字符串间的关系可以构成一棵树 于是我们先用字符串哈希建树,再树形$dp$即可 设$dp[i][j]$为第$i$个节点从$P$字符串的 ...

  3. Codeforces 914 C Travelling Salesman and Special Numbers

    Discription The Travelling Salesman spends a lot of time travelling so he tends to get bored. To pas ...

  4. 【知识点总结】NOIP前夕 2014.11.4

    2014.11.4 7:33 还有三天半就要NOIP,圈一下要背的知识点: 一.数论 1.素数判断 2.筛法求素数 3.求一个数的欧拉函数值 4.预处理欧拉函数 5.卡塔兰数递推式 6.快速幂(模素数 ...

  5. [美团 CodeM 初赛 Round A]数列互质

    题目大意: 给出一个长度为n的数列a1,a2,a3,...,an,以及m组询问(li,ri,ki),求区间[li,ri]中有多少数在该区间中的出现次数与ki互质. 思路: 莫队. f[i]记录数字i出 ...

  6. 英尺和米之间的转换 Exercise06_09

    /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:英尺和米之间的转换 * */ public class Exercise06_09 { public static void ...

  7. 求一个整数个位数之和 Exercise06_02

    import java.util.Scanner; /** * @author 冰樱梦 * 时间:2018年下半年 * 题目:求一个整数个位数之和 * */ public class Exercise ...

  8. IO流--字符流缓冲技术

    缓冲技术是为了提高数据的读写效率而提出的. (1)字符流的缓冲读 在字符流的缓冲技术中提供了一个newLine()方法,这个方法是跨平台的 在读数据的时候采用读完直接刷新的方式可以保证断电后数据不会丢 ...

  9. 美团在Redis上踩过的一些坑-3.redis内存占用飙升(转载)

     一.现象:     redis-cluster某个分片内存飙升,明显比其他分片高很多,而且持续增长.并且主从的内存使用量并不一致.   二.分析可能原因:  1.  redis-cluster的bu ...

  10. fidder模拟post提交到PHP遇到的问题

    http头必须带上Content-type: application/x-www-form-urlencoded  之后 ,php 才能接收到post数据 1. 用php://input可以很便捷的取 ...