`
frank-liu
  • 浏览: 1666000 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

leetcode: Merge Two Sorted Lists

 
阅读更多

问题描述:

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.

原问题链接:https://leetcode.com/problems/merge-two-sorted-lists/

 

问题分析

    这也是一个相对来说比较简单的问题,合并两个链表,同时要求是通过调整修改两个链表来达到最终的合并效果。所以在后面的实现里可以先定义一个临时的节点,它的next指针指向合并后的第一个节点。然后就是针对两个链表,每次判断哪个的节点小就将当前节点的next指向这个小的节点。然后再将两个节点都往后移。直到两个链表的指针同时不为null的条件不满足。这个时候可能另外一个链表还有剩下的部分,只需要判断一下将剩下的接到拼接的节点后面就可以了。

    详情见如下代码:

 

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

 

1
2
分享到:
评论

相关推荐

    LeetCode Merge 2 Sorted Lists解决方案

    LeetCode Merge 2 Sorted Lists解决方案

    leetcode中文版-LeetCode:力码

    leetcode中文版 LeetCode/Cpp 本人刷题记录在此,包含题意理解与算法思路,包含在Cpp文件内部注释,后续会持续更新。 有不懂的可以联系ji648513181,同时也欢迎志同道合O的朋友一起合作更新。 已更新剑指Offer答案...

    leetcode分类-leetcode:leetcode问题的代码

    #1:Two Sum #4:Median of Two Sorted Arrays 地图 #1:Two Sum #3:Longest Substring Without Repeating Characters #5:Longest Palindromic Substring 链表 #2:Add Two Numbers 分而治之 #53:Maximum Subarray 队列/...

    MergeTwoSortedLinkedList.java

    【Leetcode】Merge Two Sorted Lists

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode ...Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3

    程序员面试宝典LeetCode刷题手册

    第四章 Leetcode 题解 1. Two Sum 2. Add Two Numbers ...21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli

    leetcode2-Leetcode:Leetcode_answer

    leetcode 2 Leetcode答案集 关于项目: 本项目包含本人LeetCode解题的答案,全部将由JavaScript语言进行解答。并会在每个题目的文件夹中添加相关的思路解析。...Merge Two Sorted Lists JavaScript O(n)

    leetcode跳跃-leetcode:leetcode一天一次

    leetcode 跳跃 leetcode 动态规划,背包问题 01背包问题:416. Partition Equal Subset Sum 最长回文:5. Longest Palindromic ...Merge Two Sorted Lists 回文 双指针判断回文:680. 验证回文字符串 Ⅱ

    leetcode答案-LeetCode:Swift中的LeetCode

    Merge Two Sorted Lists Easy #26 Remove Duplicates from Sorted Array Easy #27 Remove Element Easy #35 Search Insert Position Easy #38 Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 ...

    leetcode双人赛-LeetCode:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring Without Repeating Characters 中等 字串 重要 Median of Two Sorted Arrays 困难 数学 ...

    leetcode中文版-LeetCode:LeetcodeC++/Java

    leetcode中文版 LeetCode # Title Chinese Tag Solution 1 Two Sum 两数之和 array,hash 2 Add Two Numbers 两数相加 math 3 Longest ...Two Sorted ...two ...Merge Two Sorted Lists 合并两个有序链表 lin

    leetcode2sumc-LeetCode:LeetCode的一些题目

    Merge Two Sorted Lists 83 Easy Remove Duplicates from Sorted List 141 Easy Linked List Cycle 160 Easy Intersection of Two Linked Lists 203 Easy Remove Linked List Elements no 206 Easy Reverse Linked ...

    leetcode添加元素使和等于-leetcode:我的leetcode解决方案

    leetcode添加元素使和等于 经验教训 一定要吧功能尽量细化为函数,这样一者做题思路比较清晰,二者可以在某些情况下直接return值。 如果输入的形式是一个序列,则可以想想:分治、动规、贪婪,一般不建议搜索,因为...

    leetcode被墙-leetcode:leetcode笔记

    easy:21.Merge_Two_Sorted_Lists return a or b 首先我是第一次见到这种写法,查看了官方文档,引用如下: The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is ev

    leetcode中325题python-leetcode:leetcode

    merge-two-sorted-lists 82 删除排序链表中的重复元素 II remove-duplicates-from-sorted-list ii 83 删除排序链表中的重复元素 remove-duplicates-from-sorted-list 86 分隔链表 partition-list 92 反转链表 II ...

    leetcodepython001-LeetCode:力码

    leetcode Python ...021_Merge_Two_Sorted_Lists 2021 年 6 月 10 日 C# 008 125_Valid_Palindrome 2021 年 6 月 12 日 C# 009 412_FizzBu​​zz 2021 年 6 月 22 日 C# 中等的 # 问题 完成日期 语

    leetcode跳跃-leetcode:leetcode解题之路

    leetcode 跳跃 leetcode 按题型标签,记录...合并K个排序链表](./Heap/merge-k-sorted-lists.md) String 字符串 [0006 Z字形变换](./String/zigzag-conversion.md) [0030 串联所有单词的子串](./String/substring

    lrucacheleetcode-leetcode-journal:记录所有LeetCode挑战的存储库

    lru缓存leetcode 力扣日记 欢迎来到我的 LeetCode 期刊库。 我的每个问题都将包括一个初始计划阶段,在这个阶段我将用我自己的话来定义问题,我将如何解决这个问题,并且所有内容都将首先用伪代码写出来。 当我解决...

    leetcode中国-cu-cafes:这是一个Java存储库。而CU是楼下的咖啡馆

    leetcode中国 cu-cafes This ...具体内容为(https://leetcode.com/problems/merge-two-sorted-lists/)。其中包含规范代码以及自己写的代码 [模块]: "Merge Two Sorted Lists" 备注:代码使用 Alibab

    leetcode答案-LeetCode-Trip:LeetCode刷题代码,大佬勿入

    Merge Two Sorted Lists] [53. Maximum Subarray] [70. Climbing Stairs] [101. Symmetric Tree] [104. Maximum Depth of Binary Tree] [121. Best Time to Buy and Sell Stock] [167. Two Sum II - Input array is ...

Global site tag (gtag.js) - Google Analytics