两个有序链表的合并

柔情只为你懂 2022-08-09 11:53 275阅读 0赞

对于两个有序链表的合并问题,在《剑指offer》中,采用的是如下递归的方式完成的,即:

  1. listNode* merge(listNode* pHead1, listNode* pHead2){
  2. if(pHead1 == NULL)
  3. return pHead2;
  4. else if (pHead2 == NULL)
  5. return pHead1;
  6. listNode* pMergedHead = NULL;
  7. if(pHead1->value < pHead2->value){
  8. pMergedHead = pHead1;
  9. pMergedHead->next = merge(pHead1->next, pHead2);
  10. }else{
  11. pMergedHead = pHead2;
  12. pMergedHead->next = merge(pHead1, pHead2->next);
  13. }
  14. return pMergedHead;
  15. }

但是,上面算法是不含头结点的。本人写了三种方法,都是带头结点的:

  1. using namespace std;
  2. struct ListNode{
  3. int data;
  4. ListNode *next;
  5. };
  6. ListNode* Create_Linklist(int *arr, int len)
  7. {
  8. ListNode* head = new ListNode();
  9. head->next = NULL;
  10. ListNode* L = head;
  11. for (int i = 0; i < len; i++)
  12. {
  13. ListNode* newn = new ListNode();
  14. newn->data = arr[i];
  15. newn->next = NULL;
  16. L->next = newn;
  17. L = L->next;
  18. }
  19. return head;
  20. }
  21. ListNode* Merge1(ListNode* &h1, ListNode* &h2) //新建链表L3作为合并后的链表
  22. {
  23. if (h1 == NULL || h1->next == NULL)return h2;
  24. if (h2 == NULL || h2->next == NULL)return h1;
  25. ListNode* L1 = h1;
  26. L1 = L1->next;
  27. ListNode* L2 = h2;
  28. L2 = L2->next;
  29. while (L1&&L2)
  30. {
  31. if (L1->data <= L2->data)L1 = L1->next;
  32. else
  33. {
  34. ListNode* temp = L2;
  35. L2 = L2->next;
  36. temp->next = L1;
  37. L1 = temp;
  38. }
  39. }
  40. return h1;
  41. }
  42. void Merge2(ListNode* &head, ListNode* h1, ListNode* h2)//链表L1作为合并后的链表
  43. {
  44. if (h1 == NULL || h1->next == NULL)head = h2;
  45. if (h2 == NULL || h2->next == NULL)head = h1;
  46. ListNode* L1 = h1->next;
  47. ListNode* L2 = h2->next;
  48. head = new ListNode();
  49. head->next = NULL;
  50. ListNode* L = head;
  51. while (L1 && L2)
  52. {
  53. if (L1->data <= L2->data)
  54. {
  55. L->next = L1;
  56. L1 = L1->next;
  57. L = L->next;
  58. L->next = NULL;
  59. }
  60. else
  61. {
  62. L->next = L2;
  63. L2 = L2->next;
  64. L = L->next;
  65. L->next = NULL;
  66. }
  67. }
  68. if (!L1)
  69. {
  70. L->next = L2;
  71. }
  72. if (!L2)
  73. {
  74. L->next = L1;
  75. }
  76. }
  77. ListNode* Merge3(ListNode* head, ListNode* h1, ListNode* h2)//利用递归实现,注意本人实现的是带头结点的两个有序链表合并
  78. {
  79. if (h1 == NULL)return h2;
  80. if (h2 == NULL)return h1;
  81. ListNode* L1 = h1;
  82. ListNode* L2 = h2;
  83. if (L1->data <= L2->data)
  84. {
  85. head = L1;
  86. head->next = Merge3(head, L1->next, L2);
  87. }
  88. else
  89. {
  90. head = L2;
  91. head->next = Merge3(head, L1, L2->next);
  92. }
  93. return head;
  94. }
  95. void Print_LinkList(ListNode* &head)
  96. {
  97. if (head)
  98. {
  99. ListNode* L = head;
  100. L = L->next;
  101. while (L)
  102. {
  103. cout << L->data;
  104. L = L->next;
  105. }
  106. }
  107. }
  108. void main(int argc, char *argv[])
  109. {
  110. int arr1[] = { 1, 3, 5, 7 };
  111. ListNode* h1 = Create_Linklist(arr1, sizeof(arr1) / sizeof(int));
  112. int arr2[] = { 2, 4, 6 };
  113. ListNode* h2 = Create_Linklist(arr2, sizeof(arr2) / sizeof(int));
  114. //ListNode* merge1 = Merge1(h1, h2); //链表L1作为合并后的链表
  115. //ListNode* merge2 = NULL;
  116. //Merge2(merge2,h1,h2); //新建链表L3作为合并后的链表
  117. ListNode* L1 = h1; ListNode* L2 = h2; //利用递归实现,注意本人实现的是带头结点的两个有序链表合并,因此需要先将L1和L2后移,并用merge3->next带人Merge3函数
  118. L1 = L1->next; L2 = L2->next;
  119. ListNode* merge3 = new ListNode();
  120. merge3->next = NULL;
  121. merge3->next = Merge3(merge3->next, L1, L2);
  122. Print_LinkList(merge3);
  123. }

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 合并有序

    合并两个有序链表 1、题目 题目要求:将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输