Series 10 wins the offer: merge two sorted lists

The examples I get this question when the lift is the list of 1: 1, 3, and 2 list: 2,4,6,8 my train of thought is: 1, based on the table of each node 2 table 1 insertion point. That list is the one time I give the newly created added two elements, namely the first two nodes of the original list. I had this idea for a long time chaotic mind, middle pointers really do not know how to get up. So I went to bed to sleep, ha ha, I am probably the world's most people will escape the ......

Read the answer using a recursive method, in fact I do when I have thought to use recursion, but useless because I generally do not like to use recursion to write code, there are two reasons, one is dead easy recursive loop, it is a the complexity of recursion is too high. But this question is really too suitable for use recursion to do recursive too simple. And in the sort of time, the answer is no method only ranked a number of times, and then the remaining two lengths ranging list might then take a recursive call. Read the answer instantly clear thinking, and soon wrote the code:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  struct ListNode {
 . 4  int Val;
 . 5  struct ListNode * Next;                             
 . 6  ListNode () {}
 . 7  };
 . 8  class Solution {
 . 9  public :
 10      ListNode the Merge * (* pHead1 ListNode, * ListNode pHead2)
 . 11      {
 12 is          IF (pHead1 == NULL) // this special data processing is also very comprehensive 
13 is              return pHead2;
 14          the else 
15             IF (pHead2 == NULL)
 16                  return pHead1;
 . 17          ListNode * P = NULL;
 18 is          IF (pHead1-> Val <pHead2-> Val) // if the first data is smaller list, a linked list first put a first node to come up as a new list, the list and add the remaining two recursive function into a first list of operators continue 
. 19          {
 20 is              P = pHead1;
 21 is              p-> Next the Merge = (pHead1-> Next, pHead2);
 22 is          }
 23 is          the else // Conversely Similarly 
24          {
 25              P = pHead2;
 26 is              p-> Next the Merge = (pHead1, pHead2-> Next);
 27         }
28         return p;
29     }
30 };
31 int main()
32 {
33     Solution so;
34     struct ListNode list[4];
35     list[0].val = 1;
36     list[0].next = &list[1];
37     list[1].val = 3;
38     list[1].next = &list[2];
39     list[2].val = 5;
40     list[2].next = &list[3];
41     list[3].val = 7;
42     list[3].next = NULL;
43     struct ListNode list1[4];
44     list1[0].val = 2;
45     list1[0].next = &list1[1];
46     list1[1].val = 4;
47     list1[1].next = &list1[2];
48     list1[2].val = 6;
49     list1[2].next = &list1[3];
50     list1[3].val = 8;
51     list1[3].next = NULL;
52     ListNode *re = so.Merge(list,list1);
53     while(re!=NULL)
54     {
55         cout << re->val << endl;
56         re= re->next;
57     }
58     return 0;
59 }

To sum up this question today:

1. a topic do not meet any brain-dead he kept thinking, a topic not worth your fees so long, if a programming problem one hour did not do it, hurry to see the answer. Difficulty of the subject on offer to prove safety is not very big, try to do more to improve the efficiency of the problem, rather than a problem with dead rivalry.

2. meet complex problems learn to break down the problem, such as the title. When I thought I was nowhere why one out of two nodes to sort it? One at a time like this can not be the standard answer that yet. This trick is called divide and conquer algorithm speaking.

3. optimism optimistic optimistic, I'm Born use.

4. a bit emotional today, emmmmmm, although engineering students, but I think with the heart of life.

Finally, I made a blog on the blog more than double-digit garden friends, happy.

Guess you like

Origin www.cnblogs.com/neverland0718/p/10988868.html