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.

题目大意:

  给定两个有序的链表,求有序合并后的链表。

思路:

  链表有序合并也是数据结构里边的经典题目了。大体思路就是分别扫描两个链表,比较两个节点的大小值,把较小的节点值尾插到结果链表屁股后边,然后再次扫描两个链表,直到其中某一个链表或者两条链表同时结束。如果是某条链表提前结束则应将另一条链表的其余节点都接到结果链表上。

代码:

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode *l3;   //最终的链表头

        if (!l1) {
            return l2;
        } else if (!l2) {
            return l1;
        }

        ListNode *pTemp1 = l1;
        ListNode *pTemp2 = l2;
        ListNode *pTemp3;   //指向结果链表的最后一个节点

        //头节点特殊处理
        if (pTemp1->val < pTemp2->val) {
            l3 = pTemp1; pTemp3 = l3;
            pTemp1 = pTemp1->next;
        } else if (pTemp1->val > pTemp2->val) {
            l3 = pTemp2; pTemp3 = l3;
            pTemp2 = pTemp2->next;
        } else {
            l3 = pTemp1; pTemp1 = pTemp1->next; pTemp3 = l3;
            pTemp3->next = pTemp2; pTemp3 = pTemp2; pTemp2 = pTemp2->next;
        }

        //一个一个节点进行扫描
        while (pTemp1 && pTemp2) {
            if (pTemp1->val < pTemp2->val) {
                pTemp3->next = pTemp1; pTemp3 = pTemp1; pTemp1 = pTemp1->next;
            } else if (pTemp1->val > pTemp2->val) {
                pTemp3->next = pTemp2; pTemp3 = pTemp2; pTemp2 = pTemp2->next;
            } else {
                pTemp3->next = pTemp1;  pTemp3 = pTemp1; pTemp1 = pTemp1->next;
                pTemp3->next = pTemp2;  pTemp3 = pTemp2; pTemp2 = pTemp2->next;
            }
        }

        //剩余节点
        if (pTemp1) {
            pTemp3->next = pTemp1;
        }else if (pTemp2) {
            pTemp3->next = pTemp2;
        }

        return l3;
    }
};

  上述代码冗余太多。。。懒得改了=、=还是看一个比较简洁的递归版吧。

递归版:

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(l1, l2->next);
            return l2;
        }
    }
};

时间: 2024-12-17 16:04:56

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

题目描述: 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

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

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,将另一个链表的

【LeetCode】21. Merge Two Sorted Lists合并两个有序链表,得到新的有序链表

一.描述: 二.思路: 两个有着相同排序类型(降序或升序)的链表,合并为一新的链表,并且要求有序(和两个子链表的排序相同): 判断2个子链表是否为空,若有空链表,则返回另一不为空的链表: 两者均不为空,判断链表结点值val的大小,(此处应该有2中排序结果,大->小 或 小->大),该题中提交只接受 小->大 类型: 故新的链表头指针指向val值较小的结点,子链表中去除已经被新链表指向的结点,再次重复该操作,即新链表头指针.next指向下一个较小值对应的结点--,即递归实现. 三.代码:

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