zoukankan      html  css  js  c++  java
  • leetcode21:合并两个有序链表

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

    示例:

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

    =========================================Python=======================================

    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, val=0, next=None):
    #         self.val = val
    #         self.next = next
    class Solution:
        def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode:
            if not l1:
                return l2
            if not l2:
                return l1
            dummyNode = cur = ListNode(0);
            while l1 and l2:
                if l1.val < l2.val:
                    cur.next = l1
                    l1 = l1.next
                else:
                    cur.next = l2
                    l2 = l2.next
                cur = cur.next
            if l1:
                cur.next = l1
            if l2:
                cur.next = l2
            return dummyNode.next

    ================================================Java=============================================

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

    =======================================================Go==================================================

    /**
     * Definition for singly-linked list.
     * type ListNode struct {
     *     Val int
     *     Next *ListNode
     * }
     */
    func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
        dummyNode := &ListNode{}
        cur := dummyNode
        for l1 != nil && l2 != nil {
            if l1.Val < l2.Val {
                cur.Next = l1
                l1 = l1.Next
            } else {
                cur.Next = l2
                l2 = l2.Next
            }
            cur = cur.Next
        }
        if l1 != nil {
            cur.Next = l1
        }
        if l2 != nil {
            cur.Next = l2
        }
        return dummyNode.Next
    }
  • 相关阅读:
    网站安全:你面临2个至关重要的挑战!
    一个只需要点 「下一步」就完成监控 Windows
    论MOBA类游戏五号位的重要性
    CloudTest 事务监控:千呼万唤始出来
    深入浅出 ES6:ES6 与 Babel / Broccoli 的联用
    精华阅读第7期|程序员职业人生规划的三点建议
    (12)打鸡儿教你Vue.js
    (11)打鸡儿教你Vue.js
    (10)打鸡儿教你Vue.js
    (9)打鸡儿教你Vue.js
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13496533.html
Copyright © 2011-2022 走看看