Java 如何合并2个链表

Java 如何合并2个链表,java,linked-list,Java,Linked List,我正在尝试一种方法,在我的编程课上合并两个链表作为家庭作业。我真的很困惑,这个方法必须有这个方法签名: public UnorderedLinkedListInt merge2(UnorderedLinkedListInt列表),因此在我的测试方法中,它看起来像这样list3=list1.merge2(list2)。当这个方法只包含一个列表而不同时包含两个列表时,我对如何实现这一点感到困惑。这是到目前为止我的代码 public class UnorderedLinkedListInt e

我正在尝试一种方法,在我的编程课上合并两个链表作为家庭作业。我真的很困惑,这个方法必须有这个方法签名:

public UnorderedLinkedListInt merge2(UnorderedLinkedListInt列表)
,因此在我的测试方法中,它看起来像这样
list3=list1.merge2(list2)
。当这个方法只包含一个列表而不同时包含两个列表时,我对如何实现这一点感到困惑。这是到目前为止我的代码

    public class UnorderedLinkedListInt extends LinkedListIntClass { 
    //Default constructor 
    public UnorderedLinkedListInt() { 
        super(); 
    }

    public boolean search(int searchItem)  { 
        LinkedListNode current; //variable to traverse the list 
        current = first; 
        while (current != null) 
            if (current.info == searchItem) 
                return true; 
            else 
               current = current.link; 
        return false; 
    }

    public void insertFirst(int newItem) { 
        LinkedListNode newNode;  //variable to create the new node 
        //create and insert newNode before first 
        newNode = new LinkedListNode(newItem, first); 
        first = newNode; 
        if (last == null) 
            last = newNode; 
        count++; 
    }

    public void insertLast(int newItem)  { 
        LinkedListNode newNode; //variable to create the new node 
        //create newNode 
        newNode = new LinkedListNode(newItem, null); 
        if (first == null) { 
            first = newNode; 
            last = newNode; 
        } 
        else { 
            last.link = newNode; 
            last = newNode;

        } 
        count++; 
    }

    public void deleteNode(int deleteItem) { 
        LinkedListNode current; //variable to traverse the list 
        LinkedListNode trailCurrent; //variable just before current 
        boolean found; 
        //Case 1; the list is empty 
        if ( first == null) 
            System.err.println("Cannot delete from an empty list."); 
        else { 
            //Case 2: the node to be deleted is first 
            if (first.info == deleteItem) { 
                first = first.link; 
                if (first == null)  //the list had only one node 
                    last = null; 
                count--; 
            } 
            else {  //search the list for the given info 
                found = false; 
                trailCurrent = first; //trailCurrent points to first node 
                current = first.link; //current points to second node 
                while (current != null && !found) { 
                    if (current.info == deleteItem) 
                        found = true; 
                    else { 
                        trailCurrent = current; 
                        current = current.link; 
                    } 
                } 
                //Case 3; if found, delete the node 
                if (found) { 
                    count--; 
                    trailCurrent.link = current.link; 
                    if (last == current)  //node to be deleted was the last node 
                       last = trailCurrent; 
                } 
                else 
                   System.out.println("Item to be deleted is not in the list."); 
            } 
        } 
    } 
    public void merge(UnorderedLinkedListInt list2){
      UnorderedLinkedListInt list1 = this;

        while (list2.first != null) {//while more data to print 
            list1.insertLast(list2.first.info);
            list2.first = list2.first.link; 
        } 
    }
    public UnorderedLinkedListInt merge2(UnorderedLinkedListInt list2){
      UnorderedLinkedListInt list3 = new UnorderedLinkedListInt(); 
      UnorderedLinkedListInt list1 = this;
      while (list1.first != null) {//while more data to print 
            list3.insertLast(list1.first.info);
            list1.first = list1.first.link; 
        } 
      while (list2.first != null) {//while more data to print 
            list3.insertLast(list2.first.info);
            list2.first = list2.first.link; 
        } 
        return list3;
    }
}

我仍然无法准确理解链表是如何工作的,任何关于如何设计此方法的建议都将不胜感激。

在类似
list1.merge2(list2)
的方法调用中,该方法将
list1
作为隐式“当前对象”接收您可以使用
参考来访问

如果需要,可以使用其他名称:

public UnorderedLinkedListInt merge2(UnorderedLinkedListInt list2){
    UnorderedLinkedListInt list1 = this;
    // now merge list1 and list2
}

在类似于
list1.merge2(list2)
的方法调用中,该方法将
list1
作为隐式“当前对象”接收,您可以使用
this
引用访问该对象

如果需要,可以使用其他名称:

public UnorderedLinkedListInt merge2(UnorderedLinkedListInt list2){
    UnorderedLinkedListInt list1 = this;
    // now merge list1 and list2
}

您的第一个列表将是
引用所指向的实际对象。

您的第一个列表将是
引用所指向的实际对象。

尝试以下操作:

 import java.io.*;
 class Node1
 {
     int data;
     Node1 link;

     public Node1()
     {
         data=0;
         link=null;
        }

     Node1 ptr,start,temp,ptr1;

     void create()throws  IOException
     {
         int n;
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter first data");
         this.data=Integer.parseInt(br.readLine());
         ptr=this;
         start=ptr;
         char ins ='y';
         do
         {
             System.out.println("Wanna Insert another node???");
             ins=(char)br.read();
             br.read();
             if(ins=='y')
             {
                 temp=new Node1();
                 System.out.println("Enter next data");
                 temp.data=Integer.parseInt(br.readLine());
                 temp.link=null;
                 ptr.link=temp;
                 temp=null;
                 ptr=ptr.link;
                }
            }while(ins=='y');
        }

     void merge()throws IOException
     {
         ptr1=this;
         ptr=this;
         Node1 t=new Node1();
         t.create();
         while(ptr1.link!=null)
         { ptr1=ptr1.link;}
         ptr1.link=t.start;
         ptr1=t=null;
        System.out.println("---------------------------"); 
        System.out.println("Merged LL :\n"); 
         while(ptr!=null)
         {
             System.out.print("-->"+ptr.data);
             ptr=ptr.link;
        }
    }
}
试试这个:

 import java.io.*;
 class Node1
 {
     int data;
     Node1 link;

     public Node1()
     {
         data=0;
         link=null;
        }

     Node1 ptr,start,temp,ptr1;

     void create()throws  IOException
     {
         int n;
         BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
         System.out.println("Enter first data");
         this.data=Integer.parseInt(br.readLine());
         ptr=this;
         start=ptr;
         char ins ='y';
         do
         {
             System.out.println("Wanna Insert another node???");
             ins=(char)br.read();
             br.read();
             if(ins=='y')
             {
                 temp=new Node1();
                 System.out.println("Enter next data");
                 temp.data=Integer.parseInt(br.readLine());
                 temp.link=null;
                 ptr.link=temp;
                 temp=null;
                 ptr=ptr.link;
                }
            }while(ins=='y');
        }

     void merge()throws IOException
     {
         ptr1=this;
         ptr=this;
         Node1 t=new Node1();
         t.create();
         while(ptr1.link!=null)
         { ptr1=ptr1.link;}
         ptr1.link=t.start;
         ptr1=t=null;
        System.out.println("---------------------------"); 
        System.out.println("Merged LL :\n"); 
         while(ptr!=null)
         {
             System.out.print("-->"+ptr.data);
             ptr=ptr.link;
        }
    }
}

使用了你的建议,效果很好,谢谢!然而,当我运行merge2方法时,我遇到了一个问题。它只将列表1添加到列表3我不确定为什么它不添加列表2?您的代码修改传递给它的列表。这是故意的吗?不,我的目标是让list2保持不变,只让list1改变。然后,您需要在新变量中维护迭代状态,而不是更新
list2。首先
。首先是什么类型的
?使用了您的建议,效果很好,谢谢!然而,当我运行merge2方法时,我遇到了一个问题。它只将列表1添加到列表3我不确定为什么它不添加列表2?您的代码修改传递给它的列表。这是故意的吗?不,我的目标是让list2保持不变,只让list1改变。然后,您需要在新变量中维护迭代状态,而不是更新
list2。首先
。第一个
的类型是什么?