2017-05-31 96 views
-2

我剛開始學習鏈接列表,需要關於這段代碼的幫助。我需要編寫一個方法,將所有項目從一個鏈接列表複製到另一個鏈接列表。 任何幫助,將不勝感激。謝謝。將項目從一個鏈接列表複製到另一個鏈接

public static ListNode copy(ListNode list){ 
    //code 
} 
+0

請參閱:爲什麼「有人能幫助我嗎?」不是一個實際的問題?(http://meta.stackoverflow.com/q/284236) – EJoshuaS

+1

你嘗試過這麼遠嗎?另外,什麼是'ListNode'? – EJoshuaS

+0

你顯然沒有谷歌它,因爲Arrays.copyAll會很容易地匹配你的搜索。 – Nathan

回答

2

單從我頭上的東西入手頂部,但如上面的評論中提到,你或許應該問更具體的問題。

class ListNode { 
    int value; 
    ListNode next; 
    public ListNode(int value) { 
     super(); 
     this.value = value; 
    } 
} 

public class Test { 

    public static ListNode copy(ListNode list){ 
     if (list == null) 
      return null; 

     ListNode res = new ListNode(list.value); 
     ListNode resTmp = res; 
     ListNode listTmp = list; 

     while (listTmp.next != null){ 
      listTmp = listTmp.next; 
      resTmp.next = new ListNode(listTmp.value); 
      resTmp = resTmp.next; 
     } 

     return res; 
    } 

    public static void main(String[] args) { 
     ListNode input = new ListNode(11); 
     input.next = new ListNode(12); 
     input.next.next = new ListNode(13); 

     ListNode output = copy(input); 

     while (output != null){ 
      System.out.println(output.value); 
      output = output.next; 
     } 
    } 

} 
+0

感謝您的幫助,對於這個糟糕的問題感到抱歉,這是我第一次發帖時將會添加更多信息,並且將來也會包含我自己的嘗試 –

相關問題