关闭

Merge Two Sorted Lists(easy)

94人阅读 评论(0) 收藏 举报
分类:

题目

       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.


题意
      链接两个有序表即对两个有序列表进行合并

分析
     只要对两个链表中的元素进行比较,然后移动即可,即把两个链表的节点拼接起来,我们可以用一个head头,将比较过后的节点接在这个head头之后。最后如果有没比较完的,说明另一个list的值全比这个list剩下的小,而且拼完了,所以可以把剩下的直接全部接上去。注意头指针的建立和空指针

实现
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */


public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode head = new ListNode(0);
    ListNode p = head;
 
    while(l1!=null||l2!=null){
        if(l1!=null&&l2!=null){
            if(l1.val < l2.val){
                p.next = l1;
                l1=l1.next;
            }else{
                p.next=l2;
                l2=l2.next;
            }
            p = p.next;
        }else if(l1==null){
            p.next = l2;
            break;
        }else if(l2==null){
            p.next = l1;
            break;
        }
    }
 
    return head.next;
}

<pre class="hljs cs"><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">Solution</span> {
    <span class="hljs-function"><span class="hljs-keyword">public</span> ListNode <span class="hljs-title">mergeTwoLists</span><span class="hljs-params">(ListNode l1, ListNode l2)</span> </span>{
        <span class="hljs-comment">// 创建一个head头,从后面开始接</span>
        ListNode head = <span class="hljs-keyword">new</span> ListNode(<span class="hljs-number">0</span>);
        ListNode curr = head;
        <span class="hljs-comment">// 依次比较拼接</span>
        <span class="hljs-keyword">while</span>(l1 != <span class="hljs-keyword">null</span> && l2 != <span class="hljs-keyword">null</span>){
            <span class="hljs-keyword">if</span>(l1.val <= l2.val){
                curr.next = l1;
                l1 = l1.next;
            } <span class="hljs-keyword">else</span> {
                curr.next = l2;
                l2 = l2.next;
            }
            curr = curr.next;
        }
        <span class="hljs-comment">// 把剩余的全拼上去</span>
        <span class="hljs-keyword">if</span>(l1 == <span class="hljs-keyword">null</span>){
            curr.next = l2;
        } <span class="hljs-function"><span class="hljs-keyword">else</span> <span class="hljs-title">if</span> <span class="hljs-params">(l2 == <span class="hljs-keyword">null</span>)</span></span>{
            curr.next = l1;
        }
        <span class="hljs-keyword">return</span> head.next;
    }
}</code>

时间复杂度o(n)

空间复杂度o(1)


 

0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

LintCode数组题总结

做算法题的时候,几乎不可避免要跟数组打交道。在LintCode上数组那一章有这么一些题目: 1)547. Intersection of Two Arrays 比较简单。要求找到2个数组的...
  • luoshengkim
  • luoshengkim
  • 2016-06-02 23:16
  • 1794

两个有序数组的中位数Median of Two Sorted Arrays(很重要)

https://leetcode.com/problems/median-of-two-sorted-arrays/ 对于一个长度为n的已排序数列a,若n为奇数,中位数为a[n / 2 + 1]...
  • gao1440156051
  • gao1440156051
  • 2016-06-21 11:11
  • 3220

LeetCodet题解--21. Merge Two Sorted Lists(合并两个排序好的链表)

链接 LeetCode题目:https://leetcode.com/problems/merge-two-sorted-lists GitHub代码:https://github.co...
  • gatieme
  • gatieme
  • 2016-04-08 11:06
  • 1440

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...
  • mccand1234
  • mccand1234
  • 2016-10-23 18:09
  • 86

[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
  • 3507

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

题目链接https://leetcode.com/problems/merge-two-sorted-lists/题目原文 Merge two sorted linked lists and re...
  • coder_orz
  • coder_orz
  • 2016-05-29 01:42
  • 1162

第六周算法分析与设计Ⅱ:Merge Two Sorted Lists

问题描述: Merge two sorted linked lists and return it as a new list. The new list should be made by sp...
  • qq_20353557
  • qq_20353557
  • 2017-04-01 11:55
  • 53

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

一、题目叙述: Merge two sorted linked lists and return it as a new list. The new list should be...
  • sunshine0_0
  • sunshine0_0
  • 2016-10-25 15:26
  • 157

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...
  • xygy8860
  • xygy8860
  • 2015-07-06 19:19
  • 679

LeetCode - Merge Two Sorted Lists

题注 这题本身不难,但是有很多细节的地方需要考虑。我感觉还是因为自己对于链表本身的操作不熟练,导致好几次上传都没有被accept。还是需要继续练习啊! 题目 Merge two sorted lin...
  • u013590498
  • u013590498
  • 2014-02-07 15:52
  • 729
    个人资料
    • 访问:22626次
    • 积分:908
    • 等级:
    • 排名:千里之外
    • 原创:58篇
    • 转载:70篇
    • 译文:1篇
    • 评论:0条