我正在尝试使用第三个引用来解决递归合并排序。我想采用第三个引用并继续按排序顺序链接两个链接列表中的节点(两个链接列表单独排序),而不创建任何额外节点。
我看到有一个引用here的递归程序。但我想尝试使用第三个参考,但我继续搞乱这个。谁能让我知道我在这里错过了什么条件?
import java.util.ArrayList;
import java.util.ListIterator;
public class MergeLinkedListsIntoExisting {
public static void main(String[] args){
Node nodeList1 = null, nodeList2 = null;
Node temp = null;
ArrayList<Integer> array1 = new ArrayList<Integer>();
array1.add(3);
array1.add(7);
array1.add(9);
ArrayList<Integer> array2 = new ArrayList<Integer>();
array2.add(1);
array2.add(2);
array2.add(8);
nodeList1 = add(nodeList1, array1);
nodeList2 = add(nodeList2, array2);
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;
Node head = node;
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
if(head==null){
head = new Node();
head.data = it.next();
head.next=null;
node = head;
}
else{
current = new Node();
current.data = it.next();
current.next = null;
node.next = current;
node = node.next;
}
}
return head;
}
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{
System.out.println("No elements in the linkedList.");
}
}
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;
}
}
解析应该在mergeTwoLists的递归调用中使用temp.next。根据需要纠正我和我的方法。
答案 0 :(得分:1)
编辑:我刚看了一遍,我认为修复代码的主要修改只是删除了temp等于null的特殊情况。
我刚刚修改了你的代码并让它运行起来。只需通过temp
而不是temp.next
,如果temp为null,则不需要特殊情况。
import java.util.ArrayList;
import java.util.ListIterator;
public class MergeLinkedListsIntoExisting {
public static void main(String[] args){
Node nodeList1 = null, nodeList2 = null;
Node temp = null;
ArrayList<Integer> array1 = new ArrayList<Integer>();
array1.add(3);
array1.add(7);
array1.add(9);
ArrayList<Integer> array2 = new ArrayList<Integer>();
array2.add(1);
array2.add(2);
array2.add(8);
nodeList1 = add(nodeList1, array1);
nodeList2 = add(nodeList2, array2);
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;
Node head = node;
ListIterator<Integer> it = list.listIterator();
while(it.hasNext()){
if(head==null){
head = new Node();
head.data = it.next();
head.next=null;
node = head;
}
else{
current = new Node();
current.data = it.next();
current.next = null;
node.next = current;
node = node.next;
}
}
return head;
}
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{
System.out.println("No elements in the linkedList.");
}
}
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;
}
答案 1 :(得分:0)
如果你想要递归方法&lt;然后尝试这个
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;
}