Merge Two Sorted Lists leetcode

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.

题目意思为合并两个链表,不能用额外的空间(新链由两个链拼接而成)

思路:

比较简单,直接定义两个指针,分别指向两条链,比较所指向节点的大小 如果指针指向的值为小的,那么就让新链指向小的,同时该指针往后移动

,新链的当前指针也往后移动。直到两条链中有一条遍历到了结尾,然后将剩余的链拼接到新链

代码如下:

<span style="font-size:18px;">/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {

    if(l1==NULL)
        return l2;
    if(l2==NULL)
        return l1;

    ListNode *p,*q;
    p=l1;
    q=l2;

    ListNode *result=new ListNode(0);
    ListNode *temp =result;
    while(p&&q)
    {
        if(p->val<q->val)
        {
            temp->next=p;
            temp=p;
            p=p->next;

        }
        else
        {
            temp->next=q;
            temp=q;
            q=q->next;
        }
    }

    if(p)
    {
        temp->next=p;
    }
    if(q)
    {
        temp->next=q;
    }
        return result->next;
    }
};</span>
时间: 2025-01-04 16:40:25

Merge Two Sorted Lists leetcode的相关文章

Merge Two Sorted Lists leetcode java

题目: 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. 题解: 这道题是链表操作题,题解方法很直观. 首先,进行边界条件判断,如果任一一个表是空表,就返回另外一个表. 然后,对于新表选取第一个node,选择两个表表头最小的那个作为新表表头,指针后挪. 然后同时遍历

Merge k Sorted Lists leetcode java

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解: Merge k sorted linked list就是merge 2 sorted linked list的变形题. 而且我们很自然的就想到了经典的Merge Sort,只不过那个是对数组进行sort.而不同的地方,仅仅是Merge两个list的操作不同. 这里来复习一下Merge

23. Merge k Sorted Lists - LeetCode

Question 23.?Merge k Sorted Lists Solution 题目大意:合并链表数组(每个链表中的元素是有序的),要求合并后的链表也是有序的 思路:遍历链表数组,每次取最小节点 Java实现: public ListNode mergeKLists(ListNode[] lists) { ListNode preHead = new ListNode(0); ListNode minNode = getMinNode(lists); ListNode tmpNode =

Merge Two Sorted Lists—LeetCode

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. https://leetcode.com/problems/merge-two-sorted-lists/ 题意就是合并两个有序的链表,说实话,这个题我一开始还真有点轻视,不就是merge sort里一步么,但是真写

Merge K Sorted Lists -- LeetCode

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:构建一个大小为K的小顶堆.每次从堆顶取走一个元素后,都将这个元素所属的链表中下一个元素加入堆中.若没有下一个元素则不添加.该过程一直持续到堆空位置.时间复杂度O(nlogk). 用优先队列实现: 用优先队列实现的声明如下: priority_queue<元素类型, 该类型的容器(如vector<

[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:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归