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 splicing together the nodes of the first two lists.

AC代码如下:

ListNode* merge(ListNode* l1,ListNode* l2,int f1,int f2)
{
    if(f1<f2)
        return merge(l2,l1,f2,f1);
    //f1>=f2
    //Insert l1 into l2
    ListNode* tmp1=l1;
    ListNode* tmp2=l2;
    for(;tmp2->next!=NULL;)
    {
        if(tmp1!=NULL && tmp1->val>=tmp2->val && tmp1->val<=tmp2->next->val )
        {
            ListNode* newNode = new ListNode(tmp1->val);
            newNode->next = tmp2->next;
            tmp2->next = newNode;
            tmp1 = tmp1->next;
        }
        else
            tmp2=tmp2->next;
    }
    tmp2->next = tmp1;
    return l2;
}
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
{
    if(l1 == NULL && l2 == NULL)
        return NULL;
    else if(l1 == NULL)
        return l2;
    else if(l2 == NULL)
        return l1;
    else
    {
        int len1=0,len2=0;
        return merge(l1,l2,l1->val,l2->val);
    }

思路比较简单,考虑到在链表头插入元素稍麻烦一点,于是比较两个链表第一个值大小,将较大值所在的链表插入到较小值所在的链表。

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

LeetCode【21】 Merge Two Sorted Lists的相关文章

【LeetCode算法-21】Merge Two Sorted Lists

LeetCode第21题 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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 翻译: 合并两个有序链表并返回

【Leetcode长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

【Leetcode】【Hard】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路: 1.先取出k个list的首元素,每个首元素是对应list中的最小元素,组成一个具有k个结点的最小堆:o(k*logk) 2.此时堆顶元素就是所有k个list的最小元素,将其pop出,并用此最小元素所在list上的下一个结点(如果存在)填充堆顶,并执行下滤操作,重新构建堆.o(logk) 3

leetcode第21题-Merge Two Sorted Lists

本题目是意思是把两个有序的链表合成一个有序的链表,考察了归并算法和链表的操作. 代码也相对比较简单,简单说一下归并函数里三个指针的作用,sum是返回的第一个指针,cur是所要返回的链表里走到的位置,put是对于取到的l1或l2里的某一个指针节点.全部的可运行代码如下: #include<stdio.h> #include<string.h> #include<stdlib.h> struct ListNode{ int value; ListNode *next; };

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

【LeetCode】Merge k Sorted Lists 解题报告

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 合并几个有序链表为一个,分析算法复杂度. [分治] 直观的想法是两两合并,有两种方法:1)list1和list2合并为newlist2,newlist2再和list3合并为newlist3,newlist3再和list4合并为newlist4--依次类推:2)list1和list2合并为li

【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

【LeetCode】【数组归并】Merge k Sorted Lists

描述 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6 思路 借鉴Merge Two Sorted Lists的解决思路,对两个数

LeetCode之“链表”:Merge Two Sorted Lists &amp;&amp; Merge k Sorted Lists

1. 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. 这道题目题意是要将两个有序的链表合并为一个有序链表.为了编程方便,在程序中引入dummy节点.具体程序如下: 1 /** 2 * Defin