[英]Clone elements of linkedlist to a new list
我在一次采訪中被問到這個問題,將鏈表 A 的元素克隆到一個新列表中。 這是我的方法,但我被拒絕了。 我確實做對了,但我不確定為什么面試官不喜歡我的方法。 關於我可以做得更好的任何提示/建議? 列表 A 有元素 [10,12,11,4,5,6] 讓我們假設。
public class CopyLinkedListElements {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.head = new Node(10);
linkedList.head.next = new Node(12);
linkedList.head.next.next = new Node(11);
linkedList.head.next.next.next = new Node(4);
linkedList.head.next.next.next.next = new Node(5);
linkedList.head.next.next.next.next.next = new Node(6);
cloneOrgList(linkedList.head);
}
public static void cloneOrgList(Node head) {
Node current = head;
Node newHead = new Node(current.data);
Node prev = newHead;
System.out.println(prev.data);
while(current != null && current.next != null) {
current = current.next;
Node newNode = new Node(current.data);
prev.next = newNode;
prev = newNode;
System.out.println(prev.data);
}
}
}
除了提到的返回值之外,循環有點混亂。 可以這樣改進:
public static Node cloneLinkedList(Node head) {
Node oldCurrent = head;
Node newHead = new Node(oldCurrent.data);
Node newCurrent = newHead;
while ((oldCurrent = oldCurrent.next) != null) {
newCurrent.next = new Node(oldCurrent.data);
newCurrent = newCurrent.next;
}
return newHead;
}
如果面試官使用“克隆”而不是“復制”這個詞,他或她可能想知道您是否知道如何正確克隆 Java 中的對象。 如果是這種情況,您需要創建一個可克隆的 class。 基本方法是實現Cloneable
標記接口並覆蓋 Object 的clone
方法。
public class MyClass implements Cloneable {
// Details of MyClass omitted
// Because Java supports covariant return types, the return type
// for the overridden clone method is the same as the class (i.e. MyClass)
// Also, the method needs to be made public instead of protected.
@Override
public MyClass clone() {
try {
return (MyClass) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError("Something went wrong. This isn't supposed to happen");
}
}
}
如果他或她只是想要一份副本,那么像您展示的方法應該沒問題,除了已經提到的:您的 function 未能返回副本。 因此,從本質上講,該方法是無用的。
最后,由於您的原始帖子聲明“克隆鏈表的元素”,因此您可以調用 Linked List 克隆方法
LinkedList<SomeClass> original = new LinkedList<>();
...
LinkedList<SomeClass> clone = (LinkedList<SomeClass>) original.clone();
同樣,如果面試官希望您復制內容,您可以執行以下操作之一:
Collections.copy(dest, source)
dest.addAll(source)
List<String> dest = source.stream().collect(Collectors.toList());
最后,第三種選擇是面試官希望你進入克隆與復制 Java 中的對象,在這種情況下你會證明兩者。 以后,在開始編碼之前,請面試官用你自己的話澄清和重述問題,以確認你正確理解了這個問題。 從你的代碼中,很明顯你誤解了面試官。
您需要克隆列表,以便返回新的引用和值。 這在cloneOrgList
方法中沒有發生。 它不返回任何內容,並且它所操作的節點的 scope 僅限於方法本身。
你需要做類似的事情
public LinkedList cloneOrgList(LinkedList orig) {
Node origCurr = orig.head;
LinkedList copy = new LinkedList();
Node newCurr = new Node(origCurr.data);
copy.head = newCurr;
while (origCurr.next != null) {
origCurr = origCurr.next;
newCurr.next = new Node(origCurr.data);
newCurr = newCurr.next;
}
return copy;
}
我認為他希望使用 clone() 方法。
請看官方文檔。 Javadoc
示例代碼:
package com.raushan.testmind;
import java.util.LinkedList;
public class TestMain {
public static void main(String args[]) {
// Creating an empty LinkedList
LinkedList<Integer> list = new LinkedList<Integer>();
// Use add() method to add elements in the list
list.add(10);
list.add(12);
list.add(11);
list.add(4);
list.add(5);
list.add(6);
// Displaying the list
System.out.println("First LinkedList:" + list);
// Creating another linked list and copying
LinkedList sec_list = new LinkedList();
sec_list = (LinkedList) list.clone();
// Displaying the other linked list
System.out.println("Second LinkedList is:" + sec_list);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.