This solution is wrong but can pass the OJ, may be the test cases is too less


  • 0
    Q
    //I think there are some problem with this method
            if(head == null) return null;
            RandomListNode newHead = new RandomListNode(head.label);
            RandomListNode newCur = newHead, cur = head;
            while(cur != null){
                if(cur.random != null){
                    RandomListNode newNode = new RandomListNode(cur.random.label);
                    newCur.random = newNode;
                }
                if(cur.next != null){
                    RandomListNode newNode = new RandomListNode(cur.next.label);
                    newCur.next = newNode;
                    newCur = newCur.next;
                }
                cur = cur.next;
            }
            return newHead;

  • 1
    W

    Yeah, I think the code may have a serious problem. The random pointer should points to the Linked List ifself. I guess the judge pass your code just because it doesn't judge whether the node which random pointers points is in the Linked List or not, the node's label instead. Thus, you will pass and got AC.


  • 0
    Q

    Good point, but this is not my code, my code is using HashMap, but I want to find a solution using constant space. This one is copied from others.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.