The two linked lists are reversed, that is, the tail pointer of the other linked chain table is reversed from the merger from the tail pointer of one linked list to the other linked list. Below, the idea and implementation code of the reversal alternation of the two linked lists are introduced in detail.
1. Problem description
Links A and B
A: 1-> 2-> 3-> 4
B: A-> B-> C-> D
Please reverse the two linked list alternately. The results of the example are as follows:
4-> D-> 3-> C-> 2-> B-> 1-> A
Node type definition is as follows:
classNode {public node next; ...} 2. Source code:
Two A and B linked lists are passed back to the linked list after processing:
Node reverse_merge (node a, node b) {// A, B all have only one node if (a.next == null) {a.next = b; Return a; Node nexta; node nextb; nextb = b.next; b.next = null; nexta = a.next; a.next = b; b = nightb; space (nexta.next! = Null) {b.next = a; A = nexta; nexta = a.next; a.next = b; b = nextb;} nextb.next = a; nexta.next = b; reTurn nexta;} Three, analysis:
The program is divided into three parts -before the WHILE cycle, the While cycle, and the While cycle.
1) Processing the previous linked list A and B
2) WHILE cycle -The core processing part of the processing process can be repeated here. Our goal is the red part, to achieve a red link mode, there are two atomic structures: dark red circle 1 and blue circle 2 2 2
However, the node where A is located in specially processed A. Only for the node where A is located, a Next = Null operation is required, which means that the first atom in 1 should be implemented outside the cycle. Pointing to 1 operation.
If you use the 2 method, you only need to put 1 finger to A out of the cycle. Therefore, the atomic structure described in 2 is used here.
Information required by atomic structure
When we go to a certain cycle, assuming the operation of the blue circle, at this time, the state of our linked list is:
The more intuitive drawing method is:
It involves 3 nodes -2,3 and C. The red part is the link method we want to do. In order to link C-> 2,3-> C, we must know that there are corresponding pointers to record their position. Therefore, before the cycle, we need to master the address of these three elements, and after processing, in the same way, the atomic structure that needs to be processed in the same way.
For example, the following method records the address of the three nodes designed in this cycle:
A, NA, and B represent the pointer or reference to the corresponding nodes.
After the processing is completed, you need to record the nodes involved in the next atomic structure in the same way, so as to ensure that the cycle can be executed according to the unified logic. Our goal is:
These assignment operations are exactly what the medium code of the cycle body does. It happens that the code is also based on the naming form specified above. There is a little difference. The NA represents the nexta in the code in the figure. In addition, NEXTB is defined as an intermediate variable in the code to record the address of the previous D node before the C-> D disconnects, because the C to 2 will lose contact D. This intermediate variable is necessary.
3) Before the whole loop -solve the problem brought by the preparatory operation
We have not handled a node A, because it is too special, and there is no suitable atomic structure that can include it. So we put it outside the cycle and prepare for the circulation. The result we want is:
After that, we can put 1, 2, and B in the cycle. Here, there are only one node A and B, which also needs to be processed separately.
4) After the whole loop -the final processing
When we find that the B linked list reaches the end, the cycle is over. But at this time, there is a handling node. In other words, the end nodes are not in the atomic structure. Our cycle will stop in this atomic structure:
As the final operation, we need to manually process the link steps of d-> 3,4-> D-this is also no way, because the treatment of the atomic structure must be found. There is no way to continue the pointer.
This is not a complete way, and there are many things that have not been processed. For example, if the input A and B are different, how to deal with it. In addition, the Node data structure is not completely defined, but this is not the focus of this article.
Through the above detailed analysis, I hope to help everyone understand the methods and implementation of the two linked list alternate mergers.