21. 合并两个有序链表

知乎ID: 码蹄疾
码蹄疾,毕业于哈尔滨工业大学。
小米广告第三代广告引擎的设计者、开发者;
负责小米应用商店、日历、开屏广告业务线研发;
主导小米广告引擎多个模块重构;
关注推荐、搜索、广告领域相关知识;

题目

将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例:

输入:1->2->4, 1->3->4
输出:1->1->2->3->4->4

分析

有序链表合并,每次都取两个中较小的一个,直到其中一个遍历到链表结尾结束遍历。
如果这时候还是剩下的元素,肯定比之前的元素都大,直接添加到链表结尾就好。

code

/**
 * 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) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode merged = null;
        ListNode head = null;
        while (l1 != null && l2 != null) {
            if (head == null) {
                if (l1.val < l2.val) {
                    merged = l1;
                    l1 = l1.next;
                } else {
                    merged = l2;
                    l2 = l2.next;
                }
                head = merged;
                continue;
            }

            if (l1.val < l2.val) {
                merged.next = l1;
                l1 = l1.next;
            } else {
                merged.next = l2;
                l2 = l2.next;
            }
            merged = merged.next;
        }

        while (l1 != null) {
            merged.next = l1;
            l1 = l1.next;
            merged = merged.next;
        }

        while (l2 != null) {
            merged.next = l2;
            l2 = l2.next;
            merged = merged.next;
        }
        return head;
    }
}

原文地址:https://www.cnblogs.com/acceml/p/9313236.html

时间: 2024-07-31 11:08:57

21. 合并两个有序链表的相关文章

[leetcode] 21. 合并两个有序链表

21. 合并两个有序链表 两个有序链表合并为一个新的有序链表 class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode ans = new ListNode(Integer.MAX_VALUE); ListNode p = ans; while (l1 != null && l2 != null) { if (l1.val < l2.val) { p.next = l1; l

LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n

【leetcode】21. 合并两个有序链表

描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 思路 别用递归,递归会慢. 其他没啥了,挺简单的. class Solution: def mergeTwoLists(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = ListNode(0) first = head while

LeetCode(21. 合并两个有序链表)

问题描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 解决方案 # encoding: utf-8 class Node(object): def __init__(self): self.val = None self.next = None def __str__(self): return str(self.v

21. 合并两个有序链表-LeetCode

心得:链表问题加头指针,头指针要加一个引用(两个引用:一个用来遍历,另一个输出头节点,注意head.next) 输出完要将后面没有添加上的节点添加上. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode mergeTwo

LeetCode 21. 合并两个有序链表(C#实现)——链表,递归,迭代

迭代:设定哨兵节点head,维护一个prev指针,每次迭代都是调整prev的next指针,判断两个链表头元素大小,将小的值接入prev节点后面,同时将接入的链表和prev后 原文地址:https://www.cnblogs.com/jhgfdm/p/12274290.html

leetcode——21. 合并两个有序链表

我还是自己没有完成. 先建立新节点,然后进行操作. # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: p=rst=ListNode(0) while True:

LeetCode - 合并两个有序链表(No.21)

21 - 合并两个有序链表 date: Dec.28th, 2019 platform: windows thinking a recursive method is implemented. code /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListN

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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 Example: Input: 1->2->4, 1-&g