#21 合并排序后的两个链表

思路

使用三个游标:cur指向合并后链表的尾部,l1,l2分别用于遍历两个链表,较小的元素增加到合并后链表。

小技巧

使用冗余的头结点可以精简地判断一下情形,其中一个链表,或两个都为空链表。

从而精简代码。

朴素代码

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode * head, *ptr;

        if (!l1 && !l2) return NULL;
        if (!l1 && l2)  return l2;
        if (l1 && !l2)  return l1;

        if (l1->val > l2->val) {
            head = l2;
            l2 = l2->next;
        } else {
            head = l1;
            l1 = l1->next;
        }
        ptr = head;
        while (l1 && l2) {
            if (l1->val <= l2->val) {
                ptr->next = l1;
                l1 = l1->next;
            } else {
                ptr->next= l2;
                l2 = l2->next;
            }
            ptr = ptr->next;
        }

        if (l1) {
            ptr->next = l1;
        } else {
            ptr->next = l2;
        }

        return head;
    }
};

优化代码

class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode dummy(0);
        ListNode *ptr = &dummy;
        while (l1 && l2) {
            if (l1->val <= l2->val) {
                ptr->next = l1;
                l1 = l1->next;
            } else {
                ptr->next= l2;
                l2 = l2->next;
            }
            ptr = ptr->next;
        }

        if (l1) {
            ptr->next = l1;
        } else {
            ptr->next = l2;
        }

        return dummy.next;
    }
};
时间: 2024-08-08 03:35:25

#21 合并排序后的两个链表的相关文章

合并排序,将两个已经排序的数组合并成一个数组

#include<iostream> #include<string.h> using namespace std; void MergeArray(int a[],int alen,int b[],int blen) { int len=alen+blen-1; alen--; blen--; while (alen>=0 && blen>=0) { if (a[alen]>b[blen]) { a[len--]=a[alen--]; }else

算法之合并排序

上篇文章讲到插入排序算法,是一个标准的增量方法:在排好的子数组后,将元素插入,形成新的数组.今天要介绍的是一种新方法:分治法. 分治法,将原问题划分成n个规模较小而结构与原问题相似的子问题:递归地解决这些子问题,然后再合并其结果,就能得到原问题的解.在每一层递归上都会有三个步骤: 分解:将原问题分解成一系列子问题: 解决:递归地解决各子问题,若子问题足够小,则直接求解: 合并:将子问题的结果合并成原问题的解. 合并排序算法完全依照了上述模式,直观的操作如下: 分解:将n个元素分成各含n/2个元素

使用合并排序和快速排序对字符串按长度排序

前段时间要对字符串集合进行按长度排序,字符串长度长的排在队列前面,最短的排在最后,可以使用两种排序方法进行排序,其中快速排序的效能会好些,但快速排序在字符串的集合非常大的时候,有时会得不到正确的结果,具体原因还不清楚. 1.合拼排序的代码 using System; using System.Collections.Generic; using System.Text; namespace FtpproxyDownRule.DBUtility { public class sortbyStrin

合并排序(分治法)

使用分治法进行合并排序,问题描述参见:https://www.cnblogs.com/jingmoxukong/p/4308823.html 算法核心: //merge_sort.h #ifndef MERGE_SORT_H #define MERGE_SORT_H template <class Type> void MergeSort(Type a[], int n); #include "merge_sort.template" #endif //merge_sort

插入排序、合并排序、堆排序和快速排序

1 * 插入排序 2 * 时间复杂度O(n2) 3 * @param array原地排序算法 4 */ 5 public void insertSort(int[] array) { 6 for (int i = 1; i < array.length; i++) { 7 int present = array[i]; 8 int position = i; 9 while (position > 0 &;&; array[position - 1] > present)

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)

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

[leetcode] 21. 合并两个有序链表

21. 合并两个有序链表 两个有序链表合并为一个新的有序链表 class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode ans = new ListNode(Integer.MAX_VALUE); ListNode p = ans; while (l1 != null && l2 != null) { if (l1.val < l2.val) { p.next = l1; l

LeetCode(21. 合并两个有序链表)

问题描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 解决方案 # encoding: utf-8 class Node(object): def __init__(self): self.val = None self.next = None def __str__(self): return str(self.v