(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.

题目要求:

合并两个有序链表

注意:

不能开辟新的结点空间

解题思路:

1、归并排序,创建一个新的头结点,从头到尾分别遍历两个链表,并依次比较其大小关系,每次将头指针指向小的那个。

2、递归思想

代码:

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

// Merge combination method
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode head(0);
        ListNode *lst;
        lst=&head;
        while(l1 && l2){
            if(l1->val<=l2->val){
                lst->next=l1;
                l1=l1->next;
            }
            else{
                lst->next=l2;
                l2=l2->next;
            }
            lst=lst->next;
        }
        if(l1)
            lst->next=l1;
        if(l2)
            lst->next=l2;
        return head.next;
    }
};

// Recursive method
class Solution {
public:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        if(l1 == NULL) return l2;
        if(l2 == NULL) return l1;

        if(l1->val < l2->val) {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        } else {
            l2->next = mergeTwoLists(l2->next, l1);
            return l2;
        }
    }
时间: 2024-11-23 19:30:39

(LeetCode 21)Merge Two Sorted Lists的相关文章

LeetCode(21) - Merge Two Sorted Lists

题目要求是,给你两个sorted LinkedList,然后把它们两个合并成一个新的LinkedList.思路很简单,就是比较ListNode L1 和 L2,哪个小就把那个node放到新的list里,并且移动相应ListNode的指针(L1或L2).注意其中一个为null,另外一个不为null的时候别漏掉就好. 代码如下: 1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val;

【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 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 本题在上一题(LeetCode 21: Merge Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个. 此题有如下几种解法: 1.最简单的方法莫过于每次都从K个链表头中找到最小的那个元素,加入结果链表中.基于此,有人通过最小堆来简化最小元素的比较. struct Compa

LeetCode OJ:Merge k Sorted Lists(归并k个链表)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类似于归并2个链表,暴力一点的方法就是,每取出一个list就与以前的list归并返回merge后list,知道所有list merge完成. 但是可惜,这样做会TLE.贴下代码先: 1 /** 2 * Definition for singly-linked list. 3 * struct List

LeetCode OJ: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. 注意题目要求合并的时候不能新建节点,直接使用原来的节点,比较简单,代码如下: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4

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

【一天一道LeetCode】#23. Merge k Sorted Lists

一天一道LeetCode系列 (一)题目 Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. (二)解题 合并K个已拍好序的链表.剑指上有合并两个已排好序的链表的算法,那么K个数,我们可以采用归并排序的思想,不过合并函数可能需要修改一下,换成合并两个已排好序的链表的方法.代码如下: /** * Definition for singly-linked

[Leetcode][Python]23: Merge k Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 23: Merge k Sorted Listshttps://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. ===Comments by

leetcode笔记: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. 二.题目分析 这道题题意是,将两个已排序的链表的各个元素进行比较并合并成一个链表,具体思路是,当某一个列表的元素比较小的话,就将其加入到输出链表中,并将该链表的指针指向链表的下一个元素.题目需要注意边界