剑指offer16:输入两个单调递增的链表,合成后的链表满足单调不减规则。

1 题目描述

  输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

2 思路与方法

  迭代法:两个链表中较小的头结点作为合并后头结点,之后依次合并两个链表中较小的结点,以此类推,最终合并剩余结点; ListNode* out_list =s->Merge(l1,l4);

  递归法:两个链表中较小的头结点作为合并后头结点,递归;(不推荐递归)

3 C++核心代码

3.1 迭代实现

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6             val(x), next(NULL) {
 7     }
 8 };*/
 9 class Solution {
10 public:
11     ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
12     {
13         ListNode* phead = new ListNode(0);
14         ListNode* list_new = phead;
15         while(pHead1 || pHead2){
16             if(pHead1==NULL){
17                 list_new->next=pHead2;
18                 break;
19             }
20             else if(pHead2==NULL){
21                 list_new->next = pHead1;
22                 break;
23             }
24             if((pHead1->val)>(pHead2->val))
25             {
26                 list_new->next = pHead2;
27                 list_new = list_new->next;
28                 pHead2=pHead2->next;
29             }
30             else{
31                 list_new->next = pHead1;
32                 list_new = list_new->next;
33                 pHead1=pHead1->next;
34             }
35         }
36         return phead->next;
37     }
38 };
View Code

3.2 递归实现

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6             val(x), next(NULL) {
 7     }
 8 };*/
 9 class Solution {
10 public:
11     ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
12     {
13         if (!pHead1)
14             return pHead2;
15         if (!pHead2)
16             return pHead1;
17  
18         if (pHead1->val < pHead2->val)
19             pHead1->next = Merge(pHead1->next, pHead2);
20         else
21             pHead2->next = Merge(pHead1, pHead2->next);
22  
23         return pHead1->val < pHead2->val ? pHead1 : pHead2;
24     }
25 };
View Code

4 完整代码

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 
 6 struct ListNode {
 7     int val;
 8     struct ListNode *next;
 9     ListNode(int x) : val(x), next(NULL) {}
10 };
11 class Solution {
12 public:
13     ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
14     {
15         ListNode* phead = new ListNode(0);
16         ListNode* list_new = phead;
17         while (pHead1 || pHead2){
18             if (pHead1 == NULL){
19                 list_new->next = pHead2;
20                 break;
21             }
22             else if (pHead2 == NULL){
23                 list_new->next = pHead1;
24                 break;
25             }
26             if ((pHead1->val)>(pHead2->val))
27             {
28                 list_new->next = pHead2;
29                 list_new = list_new->next;
30                 pHead2 = pHead2->next;
31             }
32             else{
33                 list_new->next = pHead1;
34                 list_new = list_new->next;
35                 pHead1 = pHead1->next;
36             }
37         }
38         return phead->next;
39     }
40 };
41 int main()
42 {
43     Solution *s = new Solution();
44     //vector<int> v = { 2,4,6,1,3,5,7 };
45     ListNode *l1 = new ListNode(1);
46     ListNode *l2 = new ListNode(6);
47     ListNode *l3 = new ListNode(8);
48     ListNode *l4 = new ListNode(4);
49     ListNode *l5 = new ListNode(5);
50     ListNode *l6 = new ListNode(6);
51     ListNode *l7 = new ListNode(7);
52     l1->next = l2;
53     l2->next = l3;
54     //l3->next = l4; 
55     l4->next = l5;
56     l5->next = l6;
57     l6->next = l7;
58 
59     ListNode* out_list = s->Merge(l1, l4);
60     while (out_list){
61         cout << out_list->val << " ";
62         out_list = out_list->next;
63     }
64     cout << endl;
65 
66     system("pause");
67     return 0;
68 }
View Code

参考资料

https://blog.csdn.net/ansizhong9191/article/details/80697615

猜你喜欢

转载自www.cnblogs.com/wxwhnu/p/11410122.html
今日推荐