LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)

Problem:

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.

Solution:

两个有序链表,每次取头部最小的那个元素,然后将这个元素从原来链表中去掉,依次循环

题目大意:

给定两个有序链表,要求将链表合并为一个有序链表,并且不能使用额外的空间

Java源代码(344ms):

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head,p;
        if(l1==null)return l2;
        if(l2==null)return l1;
        if(l1.val > l2.val){
            p=l2;l2=l2.next;
        }else{
            p=l1;l1=l1.next;
        }
        head=p;
        while(l1!=null && l2!=null){
            if(l1.val > l2.val){
                p.next=l2;l2=l2.next;
            }else{
                p.next=l1;l1=l1.next;
            }
            p=p.next;
        }
        if(l1==null)p.next=l2;
        else p.next=l1;
        return head;
    }
}

C语言源代码(3ms):

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) {
    struct ListNode *head,*p;
    if(l1==NULL)return l2;
    if(l2==NULL)return l1;
    if(l1->val > l2->val){
        p=l2;
        l2=l2->next;
    }else{
        p=l1;
        l1=l1->next;
    }
    head=p;
    while(l1!=NULL && l2!=NULL){
        if(l1->val > l2->val){
            p->next=l2;
            l2=l2->next;
        }else{
            p->next=l1;
            l1=l1->next;
        }
        p=p->next;
    }
    if(l1==NULL)p->next=l2;
    else p->next=l1;
    return head;
}

C++源代码(13ms):

/**
 * 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) {
        ListNode *head,*p;
        if(l1==NULL)return l2;
        if(l2==NULL)return l1;
        if(l1->val > l2->val){
            p=l2;l2=l2->next;
        }else{
            p=l1;l1=l1->next;
        }
        head=p;
        while(l1!=NULL && l2!=NULL){
            if(l1->val > l2->val){
                p->next=l2;l2=l2->next;
            }else{
                p->next=l1;l1=l1->next;
            }
            p=p->next;
        }
        if(l1==NULL)p->next=l2;
        else p->next=l1;
        return head;
    }
};

Python源代码(67ms):

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @param {ListNode} l1
    # @param {ListNode} l2
    # @return {ListNode}
    def mergeTwoLists(self, l1, l2):
        if l1==None:return l2
        if l2==None:return l1
        p=l1
        if l1.val > l2.val:
            p=l2;l2=l2.next
        else:p=l1;l1=l1.next
        head=p
        while l1!=None and l2!=None:
            if l1.val > l2.val:
                p.next=l2;l2=l2.next
            else:p.next=l1;l1=l1.next
            p=p.next
        if l1==None:p.next=l2
        else:p.next=l1
        return head
时间: 2024-10-12 13:05:17

LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)的相关文章

LeetCode 23 Merge k Sorted Lists (C,C++,Java,Python)

Problem: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Solution: 采用胜者树的方法,胜者树介绍:胜者树,假如链表数组长度为n,链表元素总个数为k,那么时间复杂度为O(k*log(n)) 题目大意: 给定一个数组有序链表(数组元素可能为空),要求将这些链表合并为一个有序的链表. Java源代码(522ms): /** * Defi

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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 Example: Input: 1->2->4, 1-&g

leetCode 21. Merge Two Sorted Lists 合并链表

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. 题目大意:合并两个有序的链表 思路:通过比较两个链表的节点大小,采用尾插法建立链表. 代码如下: /**  * Definition for singly-lin

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 should be made by splicing together the nodes of the first two lists. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不

19.1.29 [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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 1 class Solution { 2 public:

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. 代码 /** * Definition for singly-linked list. * struct ListNode

LeetCode #21 Merge Two Sorted Lists (E)

[Problem] 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. [Analysis] 没什么好说的,就是按序visit并把两个list的node接在一起. [Solution] public class Solution { public ListNode m

Java [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. 解题思路: 题目的意思是将两个有序链表合成一个有序链表. 逐个比较加入到新的链表即可. 代码如下: public static ListNode mergeTwoLists(ListNode l1, Li

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. 翻译: 把2个有序链表连接,返回一个新链表 思路: 很简单,就是遍历每个节点,小的话添加在新链表的后面.如果一个为空,则把另一个接在新链表结尾. 第二种思路就是以一个链表为基准,然后和另一个比较,如果l2的节