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 {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l2 == NULL) {
            return l1;
        }
        if(l1 == NULL) {
            return l2;
        }
        if(l1->val > l2->val) {
            ListNode* temp = l2;
            temp->next = mergeTwoLists(l1, l2->next);
            return temp;
        } else {
            ListNode* temp = l1;
            temp->next = mergeTwoLists(l1->next, l2);
            return temp;
        }
    }
};

版权声明:本文为 NoMasp柯于旺 原创文章,未经许可严禁转载!欢迎访问我的博客:http://blog.csdn.net/nomasp

时间: 2024-08-08 01:08:09

LeetCode 21 Merge Two Sorted Lists的相关文章

[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 (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 (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源代

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的节

Leetcode 21. Merge Two Sorted Lists(easy)

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 { * int val; * ListNode *next; * ListNode(i