# java - 使用第三个引用递归合并两个链接列表的排序

``````import java.util.ArrayList;
import java.util.ListIterator;

public static void main(String[] args){
Node nodeList1 = null, nodeList2 = null;
Node temp = null;
ArrayList<Integer> array1 = new ArrayList<Integer>();

ArrayList<Integer> array2 = new ArrayList<Integer>();

System.out.println("**List 1**");
print(nodeList1);
System.out.println("**List 2**");
print(nodeList2);
System.out.println("Sorted List");
Node nodeList3 = mergeTwoLists(nodeList1, nodeList2, temp);
print(nodeList3);
}
private static Node add(Node node, ArrayList<Integer> list){
Node current = node;
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
}
else{
current = new Node();
current.data = it.next();
current.next = null;
node.next = current;
node = node.next;
}
}
}

private static void print(Node node) {
if(node!=null){
while(node.next!=null){
System.out.print(node.data   " ");
node = node.next;
}
System.out.println(node.data);
}
else{
}
}

private static Node mergeTwoLists(Node nodeList1, Node nodeList2, Node temp) {
if(nodeList1 == null) return nodeList2;
if(nodeList2 == null) return nodeList1;

if(nodeList1.data <= nodeList2.data){
if(temp == null){
temp = nodeList1;
temp.next = mergeTwoLists(nodeList1.next, nodeList2, temp.next);
}
else{
System.out.println(temp.data);
temp.next = mergeTwoLists(nodeList1.next, nodeList2, temp.next);
}
}else{
if(temp == null){
temp = nodeList2;
System.out.println(temp.data);
temp.next = mergeTwoLists(nodeList1, nodeList2.next, temp.next);
}
else{
System.out.println(temp.data);
temp.next = mergeTwoLists(nodeList1, nodeList2.next, temp.next);
}
}
return temp;
}
}
``````

#### 2 个答案:

``````import java.util.ArrayList;
import java.util.ListIterator;

public static void main(String[] args){
Node nodeList1 = null, nodeList2 = null;
Node temp = null;
ArrayList<Integer> array1 = new ArrayList<Integer>();

ArrayList<Integer> array2 = new ArrayList<Integer>();

System.out.println("**List 1**");
print(nodeList1);
System.out.println("**List 2**");
print(nodeList2);
System.out.println("Sorted List");
Node nodeList3 = mergeTwoLists(nodeList1, nodeList2, temp);
print(nodeList3);
}
private static Node add(Node node, ArrayList<Integer> list){
Node current = node;
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
}
else{
current = new Node();
current.data = it.next();
current.next = null;
node.next = current;
node = node.next;
}
}
}

private static void print(Node node) {
if(node!=null){
while(node.next!=null){
System.out.print(node.data   " ");
node = node.next;
}
System.out.println(node.data);
}
else{
}
}

private static Node mergeTwoLists(Node nodeList1, Node nodeList2, Node temp) {
if(nodeList1 == null) return nodeList2;
if(nodeList2 == null) return nodeList1;

if(nodeList1.data <= nodeList2.data){

temp = nodeList1;
temp.next = mergeTwoLists(nodeList1.next, nodeList2, temp);

}else{

temp = nodeList2;
temp.next = mergeTwoLists(nodeList1, nodeList2.next, temp);
}
return temp;
}
}

public class Node{
int data;
Node next;
}
``````

``````private static Node mergeTwoLists(Node nodeList1, Node nodeList2) {
if(nodeList1 == null) return nodeList2;
if(nodeList2 == null) return nodeList1;
Node tmp = new Node();
if(nodeList1.data <= nodeList2.data){
tmp.data = nodeList1.data;
tmp.next = mergeTwoLists(nodeList1.next, nodeList2);
}else{
tmp.data = nodeList2.data;
tmp.next = mergeTwoLists(nodeList1, nodeList2.next);
}
return temp;
}
``````

0条回复