Lintcode 复制带随机指针的链表

public class 复制带随机指针的链表 {

    
    /**
     * Definition for singly-linked list with a random pointer.
     * class RandomListNode {
     *     int label;
     *     RandomListNode next, random;
     *     RandomListNode(int x) { this.label = x; }
     * };
     */
    static class RandomListNode{
        int label;
        RandomListNode next,random;
        RandomListNode (int x ){
            this.label = x;
        }
    }
    /**
     * 给出一个链表,每个节点包含一个额外增加的随机指针可以指向链表中的任何节点或空的节点。
     * 返回一个深拷贝的链表
     * @author Administrator
     *
     */
    public static  class Solution {
        /**
         * @param head: The head of linked list with a random pointer.
         * @return: A new head of a deep copy of the list.
         */
        
        public static RandomListNode copyRandomList(RandomListNode head) {
            // write your code here
            if(head == null) return head;
            //赋值所有的节点
            RandomListNode current = head;
            while(current != null){
                current = copy(current);
                current  = current.next.next;
            }
            //赋值所有的随机指针
            current = head;
            RandomListNode copyhead = current.next;
            RandomListNode copycur = copyhead;
             while(copycur != null){
                    //方法一、当前节点的赋值节点的random指针 (指向) 当前节点的random指针的赋值节点
                    if(current.random != null)
                       copycur.random = current.random.next;
                    //方法二、当前节点的赋值节点的random指针 (指向) 当前节点的random指针的赋值节点
                    /**current.nex.random = current.random.nex;**/
                        //指针向下移动
                    current = copycur.next;
                    if(copycur.next != null){
                      copycur = copycur.next.next;
                    }else{
                        copycur = null;
                    }
                }
                copycur = copyhead;
                current = head;
                while(copycur != null){
                    //将两股指针剥离开
                    current.next = copycur.next;
                    if(copycur.next != null){
                        copycur.next = copycur.next.next;
                    }
                    //指针向下移动
                    current = current.next;
                    copycur = copycur.next;
                }
                return copyhead;
        }

      
        private static RandomListNode copy(RandomListNode current) {
        
            RandomListNode copy = new RandomListNode(current.label);
            copy.next = current.next;
            current.next = copy;
            return current;
        }
        
        
    }
public static void main(String[] args) {
     RandomListNode head = new RandomListNode(1);
     RandomListNode copy = Solution.copyRandomList(head);
     while(copy != null){
         System.out.print(copy.label + "->");
         copy = copy.next;
     }
    }
      

    
}

 

posted @ 2015-10-08 11:55  √珞珈搬砖工√  阅读(62)  评论(0)    收藏  举报