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
    }
  • 相关阅读:
    《“十三五”国家信息化规划》(全文)
    新华社受权发布“十三五”规划纲要 共分为20篇(
    KDD2015,Accepted Papers
    KDD2016,Accepted Papers
    图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
    【转】34门课改变人生——牛人自学计算机总结
    北上深金融机构势力版图全揭秘20160930
    马云访澳捐2000万美元设立马云-莫利奖学金 只为报答好友肯·莫利
    提升效率
    VMware设置NAT网络
  • 原文地址:https://www.cnblogs.com/liushoudong/p/13496533.html
Copyright © 2011-2022 走看看