`

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.

题目的意思是给定两个有序的链表,将它们合并为一个有序的链表。
思路比较简单,依次比较两个节点值得大小,每次取值小的元素挂到新的链表上,下面分别列出递归实现的代码和迭代实现的代码:
递归实现:
/**
 * 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 helper = null;
        if(l1.val > l2.val) {
            helper = l2;
            helper.next = mergeTwoLists(l1, l2.next);
        } else {
            helper = l1;
            helper.next = mergeTwoLists(l1.next, l2);
        }
        return helper;
    }
}


迭代实现:
/**
 * 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 helper = new ListNode(0);
        ListNode head1 = l1;
        ListNode head2 = l2;
        while(l1 != null && l2 != null) {
            if(l1.val > l2.val) {
                helper.next = l2;
                l2 = l2.next;
            } else {
                helper.next = l1;
                l1 = l1.next;
            }
            helper = helper.next;
        }
        while (l1 != null) {
            helper.next = l1;
            l1 = l1.next;
            helper = helper.next;
        }
        while(l2 != null) {
            helper.next = l2;
            l2 = l2.next;
            helper = helper.next;
        }
        return (head1.val > head2.val) ? head2 : head1;
     }
}
分享到:
评论

相关推荐

    程序员面试宝典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

    MergeTwoSortedLinkedList.java

    【Leetcode】Merge Two Sorted Lists

    c语言-leetcode 0021-merge-two-sorted-lists.zip

    c c语言_leetcode 0021_merge_two_sorted_lists.zip

    js-leetcode题解之21-merge-two-sorted-lists.js

    js js_leetcode题解之21-merge-two-sorted-lists.js

    C语言-leetcode题解之21-merge-two-sorted-lists.c

    c语言入门 C语言_leetcode题解之21-merge-two-sorted-lists.c

    leetcode中文版-LeetCode:力码

    21.Merge Two Sorted Lists LeetCode 23.Merge k Sorted Lists(solve1) LeetCode 23.Merge k Sorted Lists(solve2) LeetCode 86.Partition List LeetCode 92.Reverse Linked List II LeetCode 138.Copy List with ...

    LeetCode 刷题汇总1

    * 合并两个排序列表(Merge Two Sorted Lists):合并两个排序列表。 * 搜索插入位置(Search Insert Position):在排序数组中搜索插入位置。 8. 动态规划: * 3Sum(3Sum):找到数组中三个元素的和等于目标值...

    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:力扣笔记

    leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add Two Numbers 中等 链结串列 重要 Longest Substring ...Two Sorted ...Merge Two Sorted Lists 简单 链结串列 重要 Remove Duplicates from

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

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

    Two Sum (Easy) 2. Add Two Numbers (Medium) 3. Longest Substring Without Repeating Characters (Medium) 7. Reverse Integer (Easy) 21. Merge Two Sorted Lists (Easy) 23. Merge k Sorted Lists (Hard) 31. ...

    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

    leetcode常见的100热点算法题

    6. **链表操作**:链表题目考察了指针操作和逻辑思维,如"Reverse Linked List"(反转链表)和"Merge Two Sorted Lists"(合并两个排序链表)。 7. **哈希表**:哈希表的高效查找和存储特性使得它在解决许多问题时...

    学习总结1

    5. **Merge Two Sorted Lists(题目21)** 将两个已排序的链表合并为一个有序链表。这可以通过创建一个新的链表节点,然后以递归或迭代的方式,每次选取两个链表当前节点中的较小值作为新节点的值,然后更新指向下...

    Leetcode book刷题必备

    20. Merge Two Sorted Lists:合并两个有序链表。 21. Add Two Numbers:模拟两数相加的过程,链表表示数字,个位对齐。 22. Swap Nodes in Pairs:在链表中交换相邻节点。 23. Merge K Sorted Lists:合并 k 个排序...

    leetcode java

    - 题目包括合并两个有序链表(Merge Two Sorted Lists)、在单链表中交换相邻节点(Swap Nodes in Pairs)。 - 有难度的链表题目则要求合并K个有序链表(Merge K Sorted Lists)、复制带有随机指针的链表(Copy List...

    常见算法题答案及解析

    20. Merge Two Sorted Lists:合并两个已排序的链表。 21. Add Two Numbers:表示两个大整数相加的链表形式。 22. Swap Nodes in Pairs:将链表中的节点两两交换。 23. Merge K Sorted Linked Lists:合并K个排序...

    :猴子:LeetCode,剑指提供刷题笔记(C / c++, Python3实现)

    LeetCode 原创文章每周最少两篇,后续最新文章会在首发,视频首发,大家可以加我进交流群,技术交流或提意见都可以,欢迎Star!...Merge Two Sorted Lists 83 * Remove Duplicates from Sorted Lis

Global site tag (gtag.js) - Google Analytics