合并两个己排好序的链表及数组

唉,这么简单的东西,说简单是简单,关键是要把这东西写得好,老少兼知。应对所有测试用例,那就有点难了吧。

话说天下之事,作于细。

我们用图来说说吧:


看合并的步骤:

(1)

(2)

(3)

(4)

源代码:

  1. #ifndef COMBINE_TWO_LIST_H
  2. #define COMBINE_TWO_LIST_H
  3. #include"reverseList.h"
  4. ListNode *combineTwoList(ListNode *alist,ListNode *blist){
  5. if(alist==NULL&&blist!=NULL){
  6. return blist;
  7. }
  8. if(alist!=NULL&&blist==NULL){
  9. return alist;
  10. }
  11. if(alist==NULL&&blist==NULL){
  12. return NULL;
  13. }
  14. ListNode *preList=alist;
  15. ListNode *bckList=blist;
  16. ListNode *rootIndex=NULL;
  17. if(preList->val>=bckList->val){
  18. rootIndex=bckList;
  19. bckList=bckList->nxt;
  20. }else{
  21. rootIndex=preList;
  22. preList=preList->nxt;
  23. }
  24. ListNode *result=rootIndex;
  25. while(preList!=NULL&&bckList!=NULL){
  26. if(preList->val>=bckList->val){
  27. rootIndex->nxt=bckList;
  28. rootIndex=rootIndex->nxt;
  29. bckList=bckList->nxt;
  30. }else{
  31. rootIndex->nxt=preList;
  32. rootIndex=rootIndex->nxt;
  33. preList=preList->nxt;
  34. }
  35. }
  36. if(preList==NULL){
  37. rootIndex->nxt=bckList;
  38. }
  39. if(bckList==NULL){
  40. rootIndex->nxt=preList;
  41. }
  42. return result;
  43. }
  44. #endif

边界条件注意:

  1. if(preList==NULL){
  2. rootIndex->nxt=bckList;
  3. }
  4. if(bckList==NULL){
  5. rootIndex->nxt=preList;
  6. }

测试:

  1. int main(){
  2. int arr1[4]={1,3,5,7};
  3. int arr2[6]={2,4,8,8,8,10};
  4. ListNode *root1=constructList(arr1,4);
  5. ListNode *root2=constructList(arr2,6);
  6. ListNode *root=combineTwoList(root1,root2);
  7. printList(root);
  8. }

其实这种思维式和合并两个己排好的数组是差不多的,大家可能知道归并排序吧,里面不是有个合并两个己排好序的

数组的操作 吗?

嗯,我们来看看,其实也是一样。

(2)

(2)

(3)

(4)

源码:

  1. #ifndef COMBINE_TWO_ARR_H
  2. #define COMBINE_TWO_ARR_H
  3. int *combineArr(int *arr1,int Len1,int *arr2,int Len2){
  4. int *arr=new int[Len1+Len2];
  5. int *arr1Iter=arr1;
  6. int *arr2Iter=arr2;
  7. int *arrIter=arr;
  8. while(arr1Iter<=arr1+Len1-1&&arr2Iter<=arr2+Len2-1){
  9. if(*arr1Iter<*arr2Iter){
  10. *arrIter=*arr1Iter;
  11. arrIter++;
  12. arr1Iter++;
  13. }else{
  14. *arrIter=*arr2Iter;
  15. arrIter++;
  16. arr2Iter++;
  17. }
  18. }
  19. if(arr1Iter>arr1+Len1-1){
  20. while(arr2Iter<=arr2+Len2-1){
  21. *arrIter=*arr2Iter;
  22. arrIter++;
  23. arr2Iter++;
  24. }
  25. }
  26. if(arr2Iter>arr2+Len2-1){
  27. while(arr1Iter<=arr1+Len1-1){
  28. *arrIter=*arr1Iter;
  29. arrIter++;
  30. arr1Iter++;
  31. }
  32. }
  33. return arr;
  34. }
  35. #endif

注边界条件:

  1. if(arr1Iter>arr1+Len1-1){
  2. while(arr2Iter<=arr2+Len2-1){
  3. *arrIter=*arr2Iter;
  4. arrIter++;
  5. arr2Iter++;
  6. }
  7. }
  8. if(arr2Iter>arr2+Len2-1){
  9. while(arr1Iter<=arr1+Len1-1){
  10. *arrIter=*arr1Iter;
  11. arrIter++;
  12. arr1Iter++;
  13. }
  14. }

来自为知笔记(Wiz)

时间: 2024-08-29 03:45:56

合并两个己排好序的链表及数组的相关文章

leetcode链表--8、merge-two-sorted-list(按顺序合并两个已经排好序的链表)

题目描述 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)定义l,每次l指向两个链表中小的那个节点,定义head为头指针,定义ptr,将l依次连成链表 2)两个链表均不为空,将其小的值赋给l 3)将其中节点多的那个链表剩余节点赋给l 4)每次l得到

【算法导论学习-016】两个已排过序的等长数组的中位数(median of two sorted arrays)

问题来源 <算法导论>P223 9.3-8: Let X[1..n] and Y[1..n] be two arrays, each containing nnumbers already in sorted order. Give an O(lgn)-time algorithm to find themedian of all 2n elements in arrays X and Y. 翻译过来即:求两个等长(n个元素)的已排序数组A和B的中位数 方案1:对两个数组进行归并直到统计到第n

合并两个已经排好序的不等长的数组

给两个已经排好序的数组,这两个数组的长度可能不相等,如何将他们合并? package airth; public class TestMergeArray { /**  * 功能:  * 作者: jiangfuqiang  * 创建日期:2014-10-13  * 修改者: mender  * 修改日期: modifydate  * @param args  */ public static void main(String[] args) { // TODO Auto-generated me

LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)

Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 简单的链表去重而已啊,遍历一边就实现了: 1 class Solution { 2 public: 3 ListNode*

解决UNION ALL合并两个结果集后排序的问题

日常开发中,如果实用UNION ALL合并两个已经排好序的结果集的时候,需求是第二个结果集数据排在第一个结果集数据下面,单纯的实用order by是无效的,因为order by的优先级比UNION ALL低. 例如: select one.*  from (select t1.* from table1 t1 where 1=1 and t1.day >3 order by t1.create_date desc)  one UNION ALL select two.*  from (selec

将两个排好序的数组,合并到另外一个数组中,并且合并之后的数组也是有序的。

int a[3] = {12, 15, 17}; int b[4] = { 2, 8, 16, 22}; int c[7] = {0}; int i = 0, j = 0, k = 0; while (i < 3 && j < 4 ) { if (a[i] > b[j]) { c[k++] = b[j++]; } else { c[k++] = a[i++]; } } while (i < 3) { c[k++] = a[i++]; } while (j <

【LeetCode-面试算法经典-Java实现】【021-Merge Two Sorted Lists(合并两个排好序的单链表)】

[021-Merge Two Sorted Lists(合并两个排好序的单链表)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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. 题目大意 合并两个排序链表并返回一个新的列表.新的链表的

Python 实现把两个排好序的的列表合并成一个排序列表

列表是生序的 # -*- coding: utf-8 -*- # 合并两个排序的数组 def merge_list(a, b): if not a: return b if not b: return a a_index = b_index = 0 ret = [] while a_index < len(a) and b_index < len(b): if a[a_index] <= b[b_index]: ret.append(a[a_index]) a_index += 1 el

编程练习:合并两个数组(包括三种快排程序)

1 #include <stdio.h> 2 #include <stdlib.h> 3 4 /*确定快排1的分界值*/ 5 int partition1(int array[],int r,int m){ 6 int i,j=m,temp,flag1=0,flag2=0;//比较的数是大数时将flag1置1,也就是当遇到大数之后,再次遇到小数才进行前后交换: 7 //flag2为0时,j=i,第一次遇到大数时,把flag2置1;也就是说,j的初始值为参考值的坐标, 8 //当遇到