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.

思路.首先判断是否有空链表,如果有,则直接返回另一个链表,如果没有,则开始比较两个链表的当前节点,返回较小的元素作为前驱,并且指针向后移动一位,再进行比较,如此循环,知道一个链表的next指向NULL,将另一个链表的后序元素进行连接即可。

迭代:

/**
 * 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;

        if(l1->val < l2->val) {
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        } else {
            l2->next = mergeTwoLists(l2->next, l1);
            return l2;
        }
    }
};

循环:

/**
 * 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* head = (l1->val > l2->val)?l2:l1;
        ListNode* temp;
        while(l1!=NULL && l2!=NULL)
        {
            while(l1->next!=NULL&&l2!= NULL &&l1->next->val <= l2->val)
            {
                l1 = l1->next;
            }
            if(l1 != NULL&&l2 !=NULL &&l1->val <= l2->val)
            {
                temp = l1->next;
                l1->next = l2;
                l1 = temp;
            }
            while(l2->next !=NULL&& l1 !=NULL &&l2->next->val <= l1->val)
            {
                l2 = l2->next;
            }
             if(l2 != NULL &&l1 != NULL&&l2->val <= l1->val)
            {
                temp = l2->next;
                l2->next = l1;
                l2 = temp;
            }

        }
        return head;
    }
};
时间: 2024-08-05 18:15:35

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

[Leetcode][Python]21: Merge Two Sorted Lists

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 21: Merge Two Sorted Listshttps://oj.leetcode.com/problems/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 th

C# 写 LeetCode easy #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. Example: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 代

[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 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

21. Merge Two Sorted Lists(js)

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题意

leedCode练题——21. Merge Two Sorted Lists(照搬大神做法)

1.题目 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-&g

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

【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. 3 * public class ListNode