Easy-题目23:21. Merge Two Sorted Lists

原创 2016年05月30日 20:08:40

题目原文:
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.
题目大意:
把两个排序的链表合并成一个链表并仍然保持有序。
题目分析:
(1) 如果一个链表是空的,则返回另一个。
(2) 令两个指针l1和l2分别指向A、B两个链表的头,扫两个链表,对应节点谁较小谁就接到新链表的尾部,若相等两个都接上。
(3) 当其中一个链表扫完,另一个未扫完的时候,把未扫完的部分接到新链表尾部。
源码:(language:java)

public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if(l1==null)
            return l2;
        else if(l2==null)
            return l1;
        else {
            ListNode head=new ListNode(Math.min(l1.val,l2.val));
            ListNode temp=head;
            if(temp.val==l1.val && temp.val==l2.val) {
                head.next=new ListNode(l1.val);
                temp=temp.next;
            }
            if(temp.val==l1.val)
                l1=l1.next;
            if(temp.val==l2.val)
                l2=l2.next;
            while(l1!=null && l2!=null) {
                if(l1.val<=l2.val) {
                    ListNode p=new ListNode(l1.val);
                    temp.next=p;
                    temp=p;
                    l1=l1.next;
                }
                else {
                    ListNode p=new ListNode(l2.val);
                    temp.next=p;
                    temp=p;
                    l2=l2.next;
                }
            }
            if(l1==null)
                temp.next=l2;
            else if(l2==null)
                temp.next=l1;
            return head;        
        }
    }
}

成绩:
1ms,beats 12.13%,众数1ms,87.87%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

21. Merge Two Sorted Lists [easy] (Python)

题目链接https://leetcode.com/problems/merge-two-sorted-lists/题目原文 Merge two sorted linked lists and re...

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)

[LeetCode] 021. Merge Two Sorted Lists (Easy) (C++/Python)
  • hcbbt
  • hcbbt
  • 2015-03-04 20:11
  • 3234

Merge Two Sorted Lists (EASY)

题目Merge two sorted linked lists and return it as a new list. The new list should be made by splicing...

LeetCode笔记:21. Merge Two Sorted Lists

合并两个有序链表

21. Merge Two Sorted Lists★

题目内容:Merge two sorted linked lists and return it as a new list. The new list should be made by splic...

Leetcode刷题记——21. Merge Two Sorted Lists(合并两有序链表)

一、题目叙述: Merge two sorted linked lists and return it as a new list. The new list should be...

LeetCode题解-21-Merge Two Sorted Lists

原题 本题较为简单,但是只想到了迭代的解法,没有想到递归。 迭代法 解题思路 比较L1与L2并增加新链表的节点,值为L1或者L2中较小的值。 代码 public c...

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

Merge Two Sorted Lists  Merge two sorted linked lists and return it as a new list. The new list sho...

LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)

Problem: Merge two sorted linked lists and return it as a new list. The new list should be made by ...

LeetCode算法题——21. Merge Two Sorted Lists

题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splici...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)