【剑指offer】面试题35:复杂链表的复制(Java 实现)
請實現函數ComplexListNode* Clone(ComplexListNode * pHead),復制一個復雜鏈表。在復雜鏈表中,每個結點除了有一個m_pNext指針指向下一個結點外,還有一個m_pSibling指向鏈表中的任意結點或者NULL。結點的C++定義如下:
struct ComplexListNode{
?? ?int m_nValue;
?? ?ComplexListNode* m_pNext;
?? ?ComplexListNode* m_pSibling;
};
上圖是一個含有5個結點的復雜鏈表。圖中實線箭頭表示m_pNext指針,虛線箭頭表示m_pSibling指針。為了簡單起見,指向NULL的指針沒有畫出。
思路:
第一步制原始鏈表上的每個結點N創建N',然后把這些創建出來的結點用m_pNext鏈接起來。同時我們把<N, N'>的配對信息放到一個哈希表中。第二步還是設置復制鏈表上的每個結點的m_pSibling。如果在原始鏈表中結點N的m_pSibling指向結點S,那么在復制鏈表中,對應的N'應該指向S'。由于有了哈希表,我們可以在O(1)的時間根據S找到S’。這種是以O(n)的空間換來了O(n)的時間復雜度。
?
代碼:
package offer;
import java.util.HashMap;
import java.util.Map;
class ComplexList
{
?? ?char val;
?? ?ComplexList next = null;
?? ?ComplexList extend = null;
?? ?ComplexList(char val)
?? ?{
?? ??? ?this.val = val;
?? ?}
}
public class ti35 {
?? ?static ComplexList CloneComplexList(ComplexList head)
?? ?{
?? ??? ?Map<ComplexList,ComplexList> map = new HashMap<ComplexList,ComplexList>();
?? ??? ?ComplexList CloneNode = new ComplexList('a');
?? ??? ?if(head!=null)
?? ??? ?{
?? ??? ??? ?CloneNode = head;
?? ??? ??? ?map.put(head,CloneNode);
?? ??? ?}
?? ??? ?ComplexList CloneHead = CloneNode;
?? ??? ?while(head.next!=null)
?? ??? ?{
?? ??? ??? ?head = head.next;
?? ??? ??? ?CloneNode = CloneNode.next;
?? ??? ??? ?map.put(head, CloneNode);
?? ??? ?}
?? ??? ?CloneNode = CloneHead;
?? ??? ?while(CloneNode.next!=null)
?? ??? ?{
?? ??? ??? ?CloneNode.extend = map.get(CloneNode.extend);
?? ??? ??? ?CloneNode = CloneNode.next;
?? ??? ?}
?? ??? ?return CloneHead;
?? ?}
?? ?public static void main(String[] args)
?? ?{
?? ??? ?ComplexList a = new ComplexList('A');
?? ??? ?ComplexList b = new ComplexList('B');
?? ??? ?ComplexList c = new ComplexList('C');
?? ??? ?ComplexList d = new ComplexList('D');
?? ??? ?ComplexList e = new ComplexList('E');
?? ??? ?a.next = b;
?? ??? ?b.next = c;
?? ??? ?c.next = d;
?? ??? ?d.next = e;
?? ??? ?b.extend = e;
?? ??? ?a.extend = c;
?? ??? ?d.extend = b;
?? ??? ?ComplexList result = CloneComplexList(a);
?? ??? ?while(result.next!=null)
?? ??? ?{
?? ??? ??? ?System.out.print(result.val+" "+result.next.val+" ");
?? ??? ??? ?if(result.extend!=null)
?? ??? ??? ?{
?? ??? ??? ??? ?System.out.println(result.extend.val);
?? ??? ??? ?}
?? ??? ??? ?else
?? ??? ??? ?{
?? ??? ??? ??? ?System.out.println();
?? ??? ??? ?}
?? ??? ??? ?result = result.next;
?? ??? ?}
?? ?}
}
不使用輔助空間
代碼:
static ComplexList CloneComplexList2(ComplexList head)
?? ?{
?? ??? ?if(head==null)
?? ??? ?{
?? ??? ??? ?return head;
?? ??? ?}
?? ??? ?ComplexList p = head;//記錄原來鏈表的頭結點
?? ??? ?while(head.next!=null)
?? ??? ?{
?? ??? ??? ?ComplexList x = new ComplexList(head.val);
?? ??? ??? ?x.next = head.next;
?? ??? ??? ?head.next = x;
?? ??? ??? ?head = x.next;
?? ??? ?}
?? ??? ?ComplexList x = new ComplexList(head.val);
?? ??? ?head.next = x;
?? ??? ?head = p;
?? ??? ?while(head.next.next!=null)
?? ??? ?{
?? ??? ??? ?if(head.extend!=null)
?? ??? ??? ?head.next.extend = head.extend.next;
?? ??? ??? ?head = head.next.next;
?? ??? ??? ?
?? ??? ?}
?? ??? ?head = p;
?? ??? ?ComplexList q = head.next,result;//記錄克隆鏈表的頭結點
?? ??? ?result = q;
?? ??? ?//System.out.println(q.val+" "+q.next.val);
?? ??? ?while(q.next!=null&&q.next.next!=null)
?? ??? ?{
?? ??? ??? ?//System.out.println(q.val+" "+q.next.val);
?? ??? ??? ?q.next = q.next.next;
?? ??? ??? ?q = q.next;
?? ??? ?}
?? ??? ?return result;
?? ?}
總結
以上是生活随笔為你收集整理的【剑指offer】面试题35:复杂链表的复制(Java 实现)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode--190. 颠倒二进制
- 下一篇: java美元兑换,(Java实现) 美元