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.

这道混合插入有序链表和我之前那篇混合插入有序数组非常的相似 (http://www.cnblogs.com/grandyang/p/4059650.html),仅仅是数据结构由数组换成了链表而已,代码写起来反而更简洁。具体思想就是新建一个链表,然后比较两个链表中的元素值,把较小的那个链到新链表中,由于两个输入链表的长度可能不同,所以最终会有一个链表先完成插入所有元素,则直接另一个未完成的链表直接链入新链表的末尾。代码如下:

/**
 * 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 *res = new ListNode(0);
        ListNode *cur = res;
        while(l1 && l2) {
            if (l1->val < l2->val) {
                cur->next = l1;
                l1 = l1->next;
            } else {
                cur->next = l2;
                l2 = l2->next;
            }
            cur = cur->next;
        }
        if (l1) cur->next = l1;
        if (l2) cur->next = l2;
        return res->next;
    }
};
时间: 2024-10-24 23:42:53

Merge Two Sorted Lists 混合插入有序链表的相关文章

[LintCode] Merge Two Sorted Lists 混合插入有序链表

Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorted list should be made by splicing together the nodes of the two lists and sorted in ascending order. Have you met this question in a real interview? Yes Exampl

21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))

题目: 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 ListNode mergeTwoLists(Li

Merge Sorted Array 混合插入有序数组

Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B are m 

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 合并两个有序链表

---恢复内容开始--- [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: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: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

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

LeetCode 21. 合并两个有序链表(Merge Two Sorted Lists)

21. 合并两个有序链表 21. Merge Two Sorted Lists 题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. LeetCode21. Merge Two Sorted Lists 示例: 输入: 1->2->4, 1->3->4 输出: 1->1->2->3->4->4 Java 实现 ListNode 类 class ListNode { int val; ListNode n