剑指offer22--链表的复制

题目:请实现函数ComplexListNode clone(ComplexListNode head),复制一个复杂链表。在复杂链表中,每个结点除了有一个next 域指向下一个结点外,还有一个sibling 指向链表中的任意结点或者null。

package 剑指offer;
/*题目:请实现函数ComplexListNode clone(ComplexListNode head),
 * 复制一个复杂链表。在复杂链表中,每个结点除了有一个next 域指向下一个结点外,
 * 还有一个sibling 指向链表中的任意结点或者null。*/
public class Test26 {
	public static void main(String args[]){
		ArrayListComp head = new ArrayListComp();
        head.value = 1;
        head.next = new ArrayListComp();
        head.next.value = 2;
        head.next.next = new ArrayListComp();
        head.next.next.value = 3;
        head.next.next.next = new ArrayListComp();
        head.next.next.next.value = 4;
        head.next.next.next.next = new ArrayListComp();
        head.next.next.next.next.value = 5;

        head.sibling = head.next.next;
        head.next.sibling = head.next.next.next.next.next;
        head.next.next.next.sibling = head.next;

        ArrayListComp tmp = head;
        printList(head);
        ArrayListComp newHead = cloneArray(head);
        printList(head);
        System.out.println(isSame(head, tmp));
        printList(newHead);
        System.out.println(isSame(head, newHead));
	}
	
	public static ArrayListComp cloneArray(ArrayListComp head){
		if(head == null){
			return null;
		}
		//先复制结点
		cloneNodes(head);
		
		//链接sibling字段
		connectNodes(head);
		
		//将链表拆分,返回复制的链接结点
		return reconnectNodes(head);
		
	}
	
    public static boolean isSame(ArrayListComp h1, ArrayListComp h2) {
        while (h1 != null && h2 != null) {
            if (h1 == h2) {
                h1 = h1.next;
                h2 = h2.next;
            } else {
                return false;
            }
        }

        return h1 == null && h2 == null;
    }
	
	public static void cloneNodes(ArrayListComp head){
		
		while(head != null){
			ArrayListComp temp = new ArrayListComp();
			temp.value = head.value;
			
			// 这两句是连接作用,将temp连接到head之后
			temp.next = head.next;
			head.next = temp;
			
			//head结点前移,变成第三个结点
			head = temp.next;
		}
	}
	
	public static void connectNodes(ArrayListComp head){
		while(head != null){
			if(head.sibling != null){
				head.next.sibling = head.sibling.next;
			}
			head = head.next.next;
		}
	} 
	
	public static ArrayListComp reconnectNodes(ArrayListComp head){
		if(head == null){
			return null;
		}
		
		ArrayListComp newhead = head.next;
		ArrayListComp pointer = newhead;
		head.next = newhead.next;
		head = head.next;
		
        while (head != null) {
            // pointer指向复制结点
            pointer.next = head.next;
            pointer = pointer.next;
            // head的下一个指向复制结点的下一个结点,即原来链表的结点
            head.next = pointer.next;
            // head指向下一个原来链表上的结点
            head = pointer.next;
        }

        // 返回复制链表的头结点
        return newhead;
	}
	
	public static void printList(ArrayListComp head) {
        while (head != null) {
            System.out.print(head.value + "->");
            head = head.next;
        }
        System.out.println("null");
    }

}

class ArrayListComp{
	int value;
	ArrayListComp next;
	ArrayListComp sibling;
}

使用先创建后复制的方式

你可能感兴趣的:(剑指offer22--链表的复制)