LeetCode_Merge Two Sorted Lists

一.题目

Merge Two Sorted Lists

Total Accepted: 63974 Total Submissions: 196044My
Submissions

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.

Show Tags

Have you met this question in a real interview?

Yes

No

Discuss

二.解题技巧

这道题就是将两个已排序的列表的元素进行比较,当某一个列表的元素比较小的话,就将其加入到输出列表中,并将该列表的指针指向列表的下一个元素。这道题是比较简单的,但是有一个边界条件要注意,就是两个列表可能会出现为空的情况,如果l1为空时,可以直接将l2进行返回;如果l2为空时,可以直接将l1返回,这样可以减少很多计算量。

三.实现代码

#include <iostream>

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/

struct ListNode
{
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution
{
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
    {
        if (!l1)
        {
            return l2;
        }

        if (!l2)
        {
            return l1;
        }

        ListNode Head(0);
        ListNode *Pre = &Head;

        while(l1 && l2)
        {
            if (l1->val < l2->val)
            {
                Pre->next = l1;
                l1 = l1->next;
                Pre = Pre->next;
            }
            else
            {
                Pre->next = l2;
                l2 = l2->next;
                Pre = Pre->next;
            }
        }

        while (l1)
        {
            Pre->next = l1;
            l1 = l1->next;
            Pre = Pre->next;
        }

        while(l2)
        {
            Pre->next = l2;
            l2 = l2->next;
            Pre = Pre->next;
        }

        return Head.next;

    }
};

四.体会

这道题主要考察的就是边界条件,主要就是处理链表为空的情况,也就是,如果l1为空,就返回l2,如果l2为空,就直接返回l1。简单的题要考虑充分啊。

版权所有,欢迎转载,转载请注明出处,谢谢

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-11 12:21:41

LeetCode_Merge Two Sorted Lists的相关文章

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 根据k个已经排好序的链表构造一个排序的链表,采用类似归并排序的算法可以通过测试 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next;

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的

LeetCodeMerge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 对每一个排序链表都设置一个指针.每次讲指针指向的单元中最小值链接,直到所有指针为空. public class Solution {    public ListNode mergeKLists(List<ListNode> lists) {                 int length =

Merge Two Sorted Lists &amp; Remove Nth Node From End of List

1.合并两个排好序的list Merge Two Sorted Lists 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. 2.删除list倒数第n个元素 Remove Nth Node From End of List Given a linked list,

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

[Leetcode][JAVA] Merge Two Sorted Lists &amp; Sort List

Merge Two Sorted Lists: 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. 两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并.如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面: 1

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

Merge Two Sorted Lists 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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

[LintCode] Merge Two Sorted Lists 混合插入有序链表

Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Have you met this question in a real interview? Yes Exampl